我正在尝试播放媒体播放器的直播mp3音频。 问题是,当我使用URL时,它会阻止UI线程一段时间然后它工作正常。但我需要它也会播放带有缓冲的歌曲。
先谢谢。请帮助。
try {
audioPlayer = new MediaPlayer();
audioPlayer.setOnBufferingUpdateListener(this);
audioPlayer.setOnCompletionListener(this);
audioPlayer.setAudioStreamType(useFrontSpeaker ? AudioManager.STREAM_VOICE_CALL : AudioManager.STREAM_MUSIC);
try {
audioPlayer.reset();
audioPlayer.setDataSource(mSongDetail.getPath());
audioPlayer.prepare();
} 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();
}
audioDuration = audioPlayer.getDuration();
audioPlayer.start();
startProgressTimer();
} catch (Exception e) {
if (audioPlayer != null) {
audioPlayer.reset();
audioPlayer.release();
audioPlayer = null;
isPaused = false;
MusicPreferance.playingSongDetail = null;
}
return false;
}
答案 0 :(得分:0)
尝试使用audioPlayer.prepareAsync()+ asyncHandler,或使用单独的线程。