My app is streaming music using Service
and MediaPlayer
, but i have a problem when trying to reset the mediaplayer to play next track. resetting mediaplayer takes long time and causes UI to freeze for a while when press next quickly multiple times although I call it in another thread.
Here is my snippet of code which i use to play track from my list
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) {
}
}
Any suggestions to avoid resetting and UI freeze problems