我的应用是使用Service
和MediaPlayer
播放音乐,但在尝试重置媒体播放器以播放下一首曲目时遇到问题。虽然我在另一个线程中调用它,但重置mediaplayer需要很长时间并导致UI快速冻结一段时间。
这是我用来从列表中播放曲目的代码片段
private void playTrack(int position) {
if (!isAudioFocusAvailable())
return;
try {
sendBufferingStarted();
// Problem is here
mPlayer.reset();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(mTracks.get(mCurrentPosition).getUrl());
mIsBuffering = true;
mPlayer.prepareAsync();
mIsPaused = false;
acquireWifiWakeLock();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
}
}
避免重置和UI冻结问题的任何建议
答案 0 :(得分:0)
尝试此代码可能有所帮助
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(!isPlaying){
mPlayer = new MediaPlayer();
mPlayer.setDataSource(soundid[position]);
mPlayer.prepare();
mPlayer.start();
isPlaying = true; // reproducir es true
}else{
isPlaying = false;
mPlayer.stop();
mPlayer.reset();
mPlayer.setDataSource(soundid[position]);
mPlayer.prepare();
mPlayer.start();
isPlaying = true;
}
mPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mPlayer) {
mPlayer.release();
isPlaying = false;
}
});
}