Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer Stop and Play

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.

like image 511
Mohammad Hammad Avatar asked Sep 04 '12 15:09

Mohammad Hammad


People also ask

How do I turn off MediaPlayer?

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.

How do I turn off MediaPlayer on Android?

1 Answer. Show activity on this post. MediaPlayer has an OnCompletionListener callback you can register to get notified when playback stops.


2 Answers

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;            }         }     } 
like image 165
Mario Avatar answered Sep 24 '22 13:09

Mario


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();     } 
like image 41
Ruchir Baronia Avatar answered Sep 22 '22 13:09

Ruchir Baronia