I have looked at various different websites looking for a solution for this problem in my code. It is a basic audio player with 3 buttons: play, pause and stop. Play and pause w
I fixed this problem by `mediaPlayer.prepare(); My problem is about live radio streaming and I achieve this problem like below code:
if (!radioIsOpen) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer
.setDataSource(URL);
mediaPlayer.prepare();
} catch (Exception e) {
}
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
Toast.makeText(getBaseContext(), "Radio is opening...", 2).show();
} else {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
Toast.makeText(getBaseContext(), "Radio is closing...", 2).show();
}
}
I fixed this problem by removing the player.stop()
which was being called when the player finished the song because of player.setOnCompletionListener()
. This has now resolved the problem and the player is working perfectly.
The problem is once you've stopped it, you setDataSource
again. You have to then call prepare()
player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
player.prepare();
Anytime you call setDataSource()
on it you will need to prepare()
it again. Think of it as loading the file, getting ready for playback.
By the way I notice you mix up calling MediaPlayer.create()
and also call setDataSource()
multiple times for the same file. There's no need to do this. Once the MediaPlayer is created and prepared (which calling .create()
does for you), you can just call .stop()
, .pause()
, .play()
on it, no need to reset it all the time.