So this is the simple code I use to play an audio file in my app:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
And here is the error log:
java.io.IOException: Prepare failed
at android.media.MediaPlayer._prepare(Native Method)
This error makes the app freeze for like 10 seconds and happens when the url
providing the mp3 file returns 404
(not found). So how can I solve the issue?
I have used mediaPlayer.prepareAsync()
, but nothing changed.