Search code examples
androidmedia-playersoundpool

Playing audio file for specific time


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.


Solution

  • 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