2012-03-14 76 views
1

我可以使用Mediaplayer播放音頻文件。但我需要播放特定時間的音頻文件。播放特定時間的音頻文件

例如:播放audio1.mp3至5分鐘。 音頻文件只有10秒,但它應該繼續播放,直到5分鐘。

回答

2

試試這個

mediaplayerplayer.setLooping(true);<--- this lets the audio to loop... 

,這是倒數計時器

MyCount counter; 
Long s1; 

counter= new MyCount(300000,1000); 
counter.start(); 

public void asdf(View v){ <---- method for onclick of buttons pause and resuming timer 
    switch(v.getId()){ 
     case R.id.button1:<-- for pause 
      counter.cancel(); 
      break; 
     case R.id.button2:<--- for resume 
      counter= new MyCount(s1,1000); 
      counter.start(); 
    } 
} 

public class MyCount extends CountDownTimer { 
    public MyCount(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() { 
     mediaplayer.stop(); 
     mediaplayer.release(); 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     s1=millisUntilFinished; 
    } 
} 

這可以幫助您暫停媒體播放器還與...一起定時器..和讓你播放歌曲的完整5分鐘

+0

counter = new MyCount(300000,1000); counter.start();這是什麼請詳細解釋 – Goofy 2012-03-14 04:51:37

+1

這是60(秒)* 5(分鐘)* 1000 ..因爲該值必須以毫秒爲單位...計數器從此值開始到1秒...倒數計時器..從5分鐘.. – 5hssba 2012-03-14 04:53:02

+0

但我要在哪裏開始mediaplayer.start(); – Goofy 2012-03-14 04:58:45

0
//decleration 
MediaPlayer mp; 
//specify the path of media file 
String filepath="?"; 

//onCreate 
    mp=new MediaPlayer(); 
Uri myUri = Uri.parse(filepath); 

       mp.setLooping(true); 
      mp = MediaPlayer.create(ActivityName.this, myUri); 
      mp.start(); 
new CountDownTimer(300000,1000) { 

      @Override 
      public void onTick(long millisUntilFinished) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onFinish() { 
       // TODO Auto-generated method stub 
if(mp.isPlaying()) 
      { 
       mp.stop(); 
      } 

      } 
     }.start();