2012-03-14 39 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();