How do I make a mediaPlayer stop when another mediaPlayer starts so that they dont play on top of each other?

≯℡__Kan透↙ 提交于 2019-12-13 07:29:25

问题


I have 2 buttons that both play an mp3. When the first song is playing I want a click of the second song to stop the first song, and start the song that was clicked. also if the first button is pressed twice the second press resets the song.

basically I don't want the songs to play on top of each other when multiple buttons are pressed.

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.placeyouknow:
    String url = "http://www.katastro.com/audio/facts/01%20That%20Place%20You%20Know.mp3";
    mp = new MediaPlayer();

try {
    mp.setDataSource(url);
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mp.prepare();
    mp.start();

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    case R.id.fallen:
    String url1 = "http://www.katastro.com/audio/fallen/01%20Fallen.mp3";
    mp = new MediaPlayer();

try {
    mp.setDataSource(url1);
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mp.prepare();
    mp.start();

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

回答1:


Seems like you need to implement a state machine to manage your player (start-stop-restart) instead of creating a new player every time a button is pressed




回答2:


Here is a sample sound helper class I use for simple apps. I use it to play at most one resource at a time within my app, but you could modify it to play audio from some url instead.

public class Sound
{
private static MediaPlayer mp = null;

/** Stop old sound and start new one */
public static void play(Context context, int resource)
{
    stop(context);

    mp = MediaPlayer.create(context, resource);
    mp.setLooping(false);
    mp.start();
}

/** Stop the music */
public static void stop(Context context)
{
    if (mp != null)
    {
        mp.stop();
        mp.release();
        mp = null;
    }
}
}



回答3:


Whoooaaaaa, losing handles on MediaPlayers left and right.

First of all, for this particular activity, you should only put

mp = new MediaPlayer();

in the onCreate() method of your activity. Delete the two you call in the onClick() method and move it to the onCreate() method.

From there, all you have to do is include a call to mp.reset() right before you call mp.setDataSource() and it should work just like you want it to!

Cheers



来源:https://stackoverflow.com/questions/6946492/how-do-i-make-a-mediaplayer-stop-when-another-mediaplayer-starts-so-that-they-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!