0

我的應用程序使用ServiceMediaPlayer流式傳輸音樂,但在嘗試重置mediaplayer以播放下一首曲目時出現問題。重置媒體播放器需要很長時間,並導致用戶界面凍結一段時間後快速按下多次,雖然我在另一個線程中調用它。MediaPlayer重置需要很長時間

這裏是我的代碼片段,我用它來從我的播放列表中的軌道

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

試試這個代碼,它可以幫助

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; 
     } 

    }); 
}