Playing audio file for specific time

2019-08-13 18:19发布

问题:

Guys i able to play audio file using Mediaplayer. But i need to play a audio file for specific time.

Eg: play audio1.mp3 till 5 minutes. The audio file is of 10 seconds only.But it should keep playing till 5 minutes.

回答1:

try this

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

and this is the countdown timer

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

this helps you to pause the mediaplayer also... along with timer.. and lets you play the song for complete 5 mins



回答2:

   //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();