• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Need help in coding

namdaemun

Lurker
Mar 11, 2011
2
0
I've just started learning to build android apps a week ago and now I'm playing with MediaPlayer class.

I'm building an app that will play a sound once the user clicks the button

here's my code :
package com.jason.shootemup;

import java.util.Random;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class game extends Activity implements OnClickListener {

View shoot, reload;
int bullet = 5;
public static final Random r = new Random();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gun);

shoot = findViewById (R.id.shoot);
reload = findViewById (R.id.reload);
reload.setVisibility(2);
shoot.setOnClickListener (this);
shoot.setOnClickListener (this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.shoot :
shoot_sequence ();
break;
case R.id.reload :
Toast.makeText(this, "Reload !!", 10);
reload_sequence();
break;
}
// TODO Auto-generated method stub

}

private void shoot_sequence() {
if (bullet > 0) {
play_shoot_sound ();
Toast.makeText(this, bullet+" left !!", 10);
bullet = bullet - 1;
}
else {
reload.setVisibility(0);
shoot.setVisibility(2);
}
// TODO Auto-generated method stub
}

private void play_shoot_sound() {
MediaPlayer mp = MediaPlayer.create(this, R.raw.shoot);
mp.start();
mp.stop();
mp.release();
// TODO Auto-generated method stub
}

private void reload_sequence() {
play_reload_sound();
bullet = r.nextInt(4) + 1;
reload.setVisibility(2);
shoot.setVisibility(0);
// TODO Auto-generated method stub
}

private void play_reload_sound() {
MediaPlayer mp = MediaPlayer.create(this, R.raw.reload);
mp.start();
mp.stop();
mp.release();
// TODO Auto-generated method stub
}
}


It was compiled successfully and worked on my emulator. but when I installed it on my motorola droid, the sound won't come out. I've set the volume to max, but nothing happens. I think it's probably because of the source code

I'll be very grateful if you can point out my mistakes in my source code

THX for the help...^^ :D:D:D
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones