I'm creating Android application contains 2 buttons, on click on each button play a mp3 file. The problem is when I play button1
it plays sound1
, when I click button2
it plays sound2
.
I check on each button the other player if it's working and I stop it and play the clicked one
But If I click on same button twice it's keep first audio playing in the background and play another one again
I tried to check isPlaying()
and to stop it, but it doesn't work!
I want If I click on button1
it play sound1
and if clicked on it again it stop it and play it again from beginning.
My code:
package com.hamoosh.playaudio; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.TextView; public class PlayaudioActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b= (Button) findViewById(R.id.button1); Button b2= (Button) findViewById(R.id.button2); final TextView t= (TextView) findViewById(R.id.textView1); final MediaPlayer mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far); final MediaPlayer mp1 = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mp1.isPlaying()) { mp1.stop(); } mp.start(); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mp.isPlaying()) { mp.stop(); } mp1.start(); } }); } }
Hope if there any better code that can use multiple buttons as an array or something to not check each button and player every time.
Calling stop() stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state. Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.
1 Answer. Show activity on this post. MediaPlayer has an OnCompletionListener callback you can register to get notified when playback stops.
You should use only one mediaplayer object
public class PlayaudioActivity extends Activity { private MediaPlayer mp; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.button1); Button b2 = (Button) findViewById(R.id.button2); final TextView t = (TextView) findViewById(R.id.textView1); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopPlaying(); mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far); mp.start(); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopPlaying(); mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet); mp.start(); } }); } private void stopPlaying() { if (mp != null) { mp.stop(); mp.release(); mp = null; } } }
To stop the Media Player without the risk of an Illegal State Exception, you must do
try { mp.reset(); mp.prepare(); mp.stop(); mp.release(); mp=null; } catch (Exception e) { e.printStackTrace(); }
rather than just
try { mp.stop(); mp.release(); mp=null; } catch (Exception e) { e.printStackTrace(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With