问题
Since the update to Android 5.0 MediaPlayer does not work properly on the Samsung Galaxy S5. The loading time is over 10 seconds after you start an audio stream.
Samplecode:
MediaPlayer mPlayer new MediaPlayer();
String url = "http://149.13.0.80:80/radio1.ogg";
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(url);
mPlayer.prepare();
mPlayer.start();
Same problem if I use MediaPlayer in a service/with prepareAsync()/other audiostreams. Nexus 4 with Android 5 has not such problems. Any solutions?
回答1:
First, your code should be surrounded in try-catch. So you might want to do an update such as
MediaPlayer mPlayer = new MediaPlayer();
String url = "http://149.13.0.80:80/radio1.ogg";
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
}
Secondly, if your prepare() is stalling you might consider using prepare() wrapped in a thread instead of prepareAsync(). That device might be doing some behaviors that are hanging, and trying to stop the MP. Adding an actionCancel might be useful.
private void actionCancel(){
try {
mp.setDataSource(new String());
} catch (Exception e) {
e.printStackTrace();
android.util.Log.d(TAG,"actionCancel(): mp.setDataSource() exception");
mp.reset();
}
}
来源:https://stackoverflow.com/questions/29023132/over-10-seconds-to-load-an-audiostream-with-mediaplayer-on-samsung-galaxy-s5-and