Austin Anderson
Austin Anderson

Reputation: 43

Android - Looping Activity to Repeat MediaPlayer

I'm trying to create a soundboard for longer audio files and can't figure out how to stop an audio file and start it again without closing the activity. Let's say each audio file is one minute long. If I play the first audio file for 20 seconds and start the next audio file, the first stops playing and the second starts playing. However, if I click the first audio file again, the second stops playing and the first does not. I need help. This is driving me insane.

    bAudio1 = (ImageButton) findViewById(R.id.bAudio1);
    bAudio2 = (ImageButton) findViewById(R.id.bAudio2);

    mpAudio1 = MediaPlayer.create(this, R.raw.audio1);
    mpAudio2 = MediaPlayer.create(this, R.raw.audio2);

    bAudio1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                    if(mpAudio1.isPlaying()) {
                            mpAudio1.stop();
                    } else {
                            if(mpAudio2.isPlaying()) { mpAudio2.stop(); }
                            mpAudio1.start();
                    }
            }
    });

    bAudio2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                    if(mpAudio2.isPlaying()) {
                            mpAudio2.stop();
                    } else {
                            if(mpAudio1.isPlaying()) { mpAudio1.stop(); }
                            mpAudio2.start();
                    }
            }
    });

Upvotes: 1

Views: 1876

Answers (1)

Swifty McSwifterton
Swifty McSwifterton

Reputation: 2667

This worked for me:

public class ExampleActivity extends Activity implements OnClickListener {

private Button button1;
private Button button2;
private MediaPlayer mediaPlayer1;
private MediaPlayer mediaPlayer2;

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

    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(this);
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        // play first audio clip and stop the second if playing
        if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
            mediaPlayer2.stop();
            mediaPlayer2.release();
            mediaPlayer2 = null;
        }
        mediaPlayer1 = MediaPlayer.create(this, R.raw.song1);
        mediaPlayer1.start();
        break;
    case R.id.button2:
        // play second audio clip and stop the first if playing
        if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
            mediaPlayer1.stop();
            mediaPlayer1.release();
            mediaPlayer1 = null;
        }
        mediaPlayer2 = MediaPlayer.create(this, R.raw.song2);
        mediaPlayer2.start();
        break;
    }
}

@Override
protected void onPause() {
    super.onPause();

    // stop the second if playing
    if (mediaPlayer2 != null && mediaPlayer2.isPlaying()) {
        mediaPlayer2.stop();
        mediaPlayer2.release();
        mediaPlayer2 = null;
    }

    // stop the first if playing
    if (mediaPlayer1 != null && mediaPlayer1.isPlaying()) {
        mediaPlayer1.stop();
        mediaPlayer1.release();
        mediaPlayer1 = null;
    }
}

Upvotes: 1

Related Questions