2013-04-26 129 views
0

我在android中製作一個應用程序,我的問題是,當我播放流時它播放的很好,但是當我點擊停止按鈕然後點擊播放按鈕時,歌曲不播放,該怎麼做?如果有人知道,請幫助我。下面是我寫的代碼: -如何在android中停止播放後再次播放流?

// method for play stream after stop it. 
public void startradio(View v) { 
     try{ 
      if(mp.isPlaying()){ 
       return; 
      } 
       mp.start(); 
     } catch(IllegalStateException ex){ 
      ex.printStackTrace(); 
     } 
    } 

// method for stop stream. 
public void stopradio(View v) { 
    if(mp.isPlaying()){ 
     mp.stop(); 
    } 
    mp.release(); 
} 
+0

你釋放上停止流,不釋放它,當你打停,你應該能夠再次啓動它。 – Eluvatar 2013-04-26 22:29:03

回答

0

@ Eluvatar的評論是非常接近,但沒有雪茄。

如果你這樣做,你的應用程序仍然會處於錯誤的狀態,並且需要在再次調用start()之前調用prepare()。你應該看看the event cycle for MediaPlayer

換句話說可能的變化將是

// method for play stream after stop it. 
public void startradio(View v) { 
     try{ 
      if(mp.isPlaying()){ 
       return; 
      } 
      mp.prepare(); 
      mp.start(); 
     } catch(IllegalStateException ex){ 
      ex.printStackTrace(); 
     } 
    } 

// method for stop stream. 
public void stopradio(View v) { 
    if(mp.isPlaying()){ 
     mp.stop(); 
    } 

} 
相關問題