2017-03-09 69 views
0

我的MediaPlayer在Android的播放聲音,我們希望播放文件基礎上開始時間和結束時間 例如:如何以編程方式在給定時間暫停/停止MediaPlayer Android?

start 10370 ,end: 14759 [OR] start 4754 ,end: 7836 

我用我的問題這個代碼,但它不工作完美 Pause/Stop MediaPlayer Android at given time programmatically

我的代碼:

mediaPlayer.setDataSource(fd); 
mediaPlayer.prepare(); 
mediaPlayer.seekTo(words.getW_start()); 
mediaPlayer.start(); 
handler.postDelayed(stopPlayerTask, words.getW_end()); 

你知道爲什麼這段代碼不起作用嗎?

回答

0

我能解決問題.complete代碼:

mediaPlayer.setDataSource(fd); 
mediaPlayer.prepare(); 
mediaPlayer.seekTo(words.getW_start()); 
mediaPlayer.start(); 
new CountDownTimer(endTime, 10) { 
public void onTick(long millisUntilFinished) { 

if(MediaPlayerUtility.getTime(mediaPlayer)>=endTime){ 
     mediaPlayer.stop(); 
    } 

     } 

    public void onFinish() { 


          } 
    }.start(); 

和MediaPlayerUtility類:

public class MediaPlayerUtility { 


    public static long getTime(MediaPlayer mediaPlayer) { 


     long totalDuration = mediaPlayer.getDuration(); // to get total duration in milliseconds 

     long currentDuration = mediaPlayer.getCurrentPosition(); // to Gets the current playback position in milliseconds 

     return currentDuration; 

    } 


}