Android - Looping Activity to Repeat MediaPlayer

Posted by Austin Anderson on Stack Overflow See other posts from Stack Overflow or by Austin Anderson
Published on 2012-04-07T03:38:07Z Indexed on 2012/04/07 5:29 UTC
Read the original article Hit count: 198

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

Thanks.

© Stack Overflow or respective owner

Related posts about android

Related posts about loops