Question

below is my code for media player. It doesn't start again after I stop. How to solve this? Even though I have called the prepare method before calling the start method. Also why doesn't the music stop playing even after I exit the programme. Please advice.

   public class MainActivity extends Activity {

Button play,pause,stop;
CheckBox c1,c2,c3;
MediaPlayer song1,song2,song3;
AlertDialog.Builder builder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    play=(Button)findViewById(R.id.button1);
    pause=(Button)findViewById(R.id.button2);
    stop=(Button)findViewById(R.id.button3);

    c1=(CheckBox)findViewById(R.id.checkBox1);
    c2=(CheckBox)findViewById(R.id.checkBox2);
    c3=(CheckBox)findViewById(R.id.checkBox3);

    song1=MediaPlayer.create(this, R.raw.finalcountdown);
    song2=MediaPlayer.create(this, R.raw.invincible);
    song3=MediaPlayer.create(this, R.raw.somewhereibelong);

    builder=new AlertDialog.Builder(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void start(View view) throws IllegalStateException, IOException
{
    if(c1.isChecked())
    {
        if(!song1.isPlaying())
        {
            song1.prepare();
            song1.start();
        }
        else{
            song1.start();
        }
    }
    if(c2.isChecked())
    {
        if(!song2.isPlaying())
        {
            song2.prepare();
            song2.start();
        }
        else{
            song2.start();
        }
    }
    if(c3.isChecked())
    {
        if(!song3.isPlaying())
        {
            song3.prepare();
            song3.start();
        }
        else{
            song3.start();
        }
    }
    else{
        builder.setMessage("Please mark a checkbox");
    }
}

public void stop(View view)
{
    if(c1.isChecked())
        song1.stop();
    else if (c2.isChecked())
        song2.stop();
    else if (c3.isChecked())
        song3.stop();
    else
        builder.setMessage("Error message ");
}

public void pause(View view)
{
    if(c1.isChecked())
    {
        song1.pause();
    }
    else if(c2.isChecked())
    {
        song2.pause();
    }
    else if(c3.isChecked())
    {
        song3.pause();
    }
    else
        builder.setMessage("error message");
}

}
Was it helpful?

Solution

you need to put song1.release() and release methods for other songs inside the stop method. Refer the life cycle of media player for further info. http://developer.android.com/reference/android/media/MediaPlayer.html

OTHER TIPS

You have to release the player OnPause or OnDestroy method.

@Override
public void onDestroy() {
    if(c1!= null)c1.release();
    else if (c2!= null)c2.release();
    else if (c3!= null)c3.release();
    else
        //something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top