how to add soundeffects when clicking a button - android

I'm having a bit of a trouble,, here's the gen. idea,, I have created an app a simple game-like application.. however the problem is when I try to click the button in the title screen it doesn't play the click sound.. can anyone please help me.. here's the code for my sound
public class Effects extends Activity implements MediaPlayer.OnCompletionListener {
Button mPlay;
MediaPlayer mPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlay = (Button)findViewById(R.id.btnStart);
mPlay.setOnClickListener(playListener);
setContentView(R.layout.activity_main);
}
#Override
public void onDestroy() {
super.onDestroy();
if(mPlayer != null) {
mPlayer.release();
}
}
private View.OnClickListener playListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mPlayer == null) {
try {
mPlayer = MediaPlayer.create(Effects.this, R.raw.button11);
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
} else {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
};
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
}
and here's my code for the main java that I want the sound to be played when I clicked this button
public class Main extends Activity implements OnClickListener
{
Button about;
Button Quit;
Button start;
protected void onCreate(Bundle onSavedInstanceState) {
super.onCreate(onSavedInstanceState);
setContentView(R.layout.activity_main);
about = (Button)findViewById(R.id.btnAbout);
about.setOnClickListener(this);
Quit = (Button)findViewById(R.id.btnQuit);
Quit.setOnClickListener(this);
start = (Button)findViewById(R.id.btnStart);
start.setOnClickListener(this);
}
public void onClick(View argo)
{
if(argo.getId()==R.id.btnAbout)
{
Intent i = new Intent(this,About.class);
this.startActivity(i);
}
if(argo.getId()==R.id.btnQuit)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
if(argo.getId()==R.id.btnStart)
{
Intent start = new Intent(this,Howto.class);
this.startActivity(start);
Intent svc=new Intent(this,Effects.class);
startService(svc);
}
}
}

Use SoundPool to play your sounds: http://developer.android.com/reference/android/media/SoundPool.html you should init sounds in your onCreate() and then play them back in onClick()
And instead of doing thousands of pointless if(argo.getId()==R.id.btnQuit) (you should at least use else) get familiar with switch/case syntax)

Related

How to stop a MediaPlayer when I go to next activity in Android studio

public class momtahina5 extends AppCompatActivity {
private static final String TAG = "momtahina5";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_momtahina5);
Log.d(TAG,"onCreate: Starting.");
Button gotobakara201 = (Button) findViewById(R.id.gotobakara201);
Button backtobakara286 = (Button) findViewById(R.id.backtobakara286);
gotobakara201.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked btngotobakara201");
Intent intent1 = new Intent(momtahina5.this,bakara201.class);
startActivity(intent1);
MediaPlayer mp = MediaPlayer.create(momtahina5.this,R.raw.mumfive);
mp.start();
}
});
}
}
What you can do is Release media player when activity is finished or destroyed.
below code will demonstrate you.
#Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
}
}
override the 'onPause()' method of the current Activity and in that do this:
#Override
public void onPause() {
super.onPause();
if(mp != null) {
mp.release();
mp = null;
}
}
And also you want to make the MediaPlayer object as a class variable.
*Edit: It would be better to use the onPause() method instead of the onDestroy() method. When you move to another activity, the previous activity is not immediately destroyed. Refer to Android activity lifecycle for more info.

Is this a good way to use MediaPlayer?

I think this is a decent way to use MediaPlayer for a Button or one time use. Am I right? Is the try block necessary? And what should I be trying to catch here? I'm really having a hard time finding a rock solid way to play a sound once.
// Button sound
private void playButtonSound() {
try{
final MediaPlayer startPlayer = MediaPlayer.create(
getApplicationContext(), R.raw.sound);
startPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
startPlayer.release();
}
});
startPlayer.start();
} catch(Throwable t){}
}
I use it like that:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.tosse);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}

how do i stop background music when back button is pressed?

i have 2 activities. and 2 different background music plays on both. when i press a button on the first activity it will go to this activity and the changing of music goes smoothly but i want to stop background music when back button is pressed. i tried this but it force closes. is there any other way to do this?
public class Categories extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
mp = MediaPlayer.create(Categories.this, R.raw.looney);
mp.setLooping(true);
mp.start();
Button cartoonButton = (Button) findViewById(R.id.cartoon);
cartoonButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchCartoon = new Intent(Categories.this, Cartoon.class);
startActivity(launchCartoon);
}
});
Button superButton = (Button) findViewById(R.id.hero);
superButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent launchSuperheroes = new Intent(Categories.this, SuperHeroes.class);
startActivity(launchSuperheroes);
}
});
Button singerButton = (Button) findViewById(R.id.singer);
singerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchSinger = new Intent(Categories.this, Singer.class);
startActivity(launchSinger);
}
});
Button famousButton = (Button) findViewById(R.id.famous);
famousButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchFamous = new Intent(Categories.this, Famous.class);
startActivity(launchFamous);
}
});
}
#Override
protected void onResume() {
super.onResume();
mp.start();
}
#Override
protected void onPause() {
super.onPause();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
You can try this code:
#Override
public void onBackPressed() {
if (player.isPlaying()) {
player.stop();
}
player.release();
super.onBackPressed();
}
Hope this help you :)

How to add soundButton(on&off) on a activity in android apps?

I Have created a branch activity .Now i wanted to add two button on that branch activity.
When i click on 'sound on' button then my beep sound on start and when i clicked on 'sound off' then my beep sound off. and also they hide simultaneously.
Thank's
MY Code on Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_layout);
soundBttnOn =(Button) findViewById(R.id.soundBttnOn);
soundBttnOn.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
startMediaPlayer();
}
}
);
soundBttnoff =(Button) findViewById(R.id.soundBttnOff);
soundBttnoff.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
stopMediaPlayer();
}
}
);
}
private void startMediaPlayer() {
mediaPlayer = MediaPlayer.create(SoundLayout.this,R.raw.keybutton5);
mediaPlayer.start();
}
private void stopMediaPlayer() {
if( mediaPlayer != null ) {
MediaPlayer mp = null;
mp.stop();
mp.release();
}
}
It showing no problem but it is not working too..:P..I am not able to implement sound.
You can do simple google search to find tons of samples for adding button. However for playing sound file check out the MediaPlayer class.
Button startBtn, stopBtn;
//get the reference of Button
....
final MediaPlayer mp = new MediaPlayer();
startBtn.onClickListener() {
public void onClick(View v) {
try {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
stopBtn.onClickListener() {
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
//hide buttons
stopBtn.setVisibiltiy(View.GONE);
startBtn.setVisibility(View.GONE);
}
}
};
PS: For only on and off, you don't need two buttons, you could do with one button.
EDIT:
For single button, just use the playing state of Media player for deciding about the action to take on button click.
singleBtn.onClickListener() {
public void onClick(View v) {
try {
if(mp.isPlaying()) mp.stop();
else {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};

Service and background music codes -updated-

i am making a quiz game that has a background music.i saw a very helpful source code using service and background music here http://marakana.com/forums/android/examples/60.html but my problem is i want to add another sound on my EasyOne.class in my correct answer button. what will i add or edit to make it work. my point is. everytime i click the a button of my easyone.class it will play a sound and if i want to turn off all my music even the corrrect button and the background music it will turn off if ill go to my settings.class and click the OFF button. hope you can help me.really appriciate it.
here is my code for my service
public class MusicService extends Service {
private static final String TAG = "";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "ON", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.bsound);
player.setLooping(false);
}
#Override
public void onDestroy() {
Toast.makeText(this, "OFF", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "ON", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
and here is my for my settings
public class Settings extends Activity implements OnClickListener {
private static final String TAG = "";
Button on, off, back;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
back = (Button) findViewById(R.id.btn_backk);
on = (Button) findViewById(R.id.btn_on);
off = (Button) findViewById(R.id.btn_off);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Back",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
}
});
on.setOnClickListener(this);
off.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.btn_on:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MusicService.class));
break;
case R.id.btn_off:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MusicService.class));
break;
}
}
}
my EasyOne.class - (class of my level 1 easy mode)
public class EasyOne extends Activity {
ImageButton a, b, c;
Intent intent ;
CountDownTimer cdt;
TextView timer;
MediaPlayer clap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (ImageButton) findViewById(R.id.ib_a);
b = (ImageButton) findViewById(R.id.ib_b);
c = (ImageButton) findViewById(R.id.ib_c);
timer = (TextView) findViewById(R.id.tv_timer);
cdt = new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
timer.setText("TIMES UP!");
intent = new Intent(getApplicationContext(),TimesUp.class);
startActivity(intent);
}
};
intent = new Intent(getApplicationContext(),ChoiceTwo.class);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intent);
//i want to add a sound here that will play if they click this button and can turn it OFF on the setting.class OFF button
cdt.cancel();
Intent intent = new Intent(getApplicationContext(),ChoiceTwo.class);
startActivity(intent);
}
});
cdt.start();
}
}
In the main activity of your code (where the game actually starts), put:
startService(new Intent(MusicService.class))
This will start your music playing as soon as the game starts

Categories

Resources