Android MediaPlayer Stop and Play

Mohammad Hammad picture Mohammad Hammad · Sep 4, 2012 · Viewed 104.5k times · Source

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.

Answer

Mario picture Mario · Sep 4, 2012

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;
           }
        }
    }