Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how can play song for 30 seconds only in mediaPlayer

Tags:

android

I am working on Android , I am creating a player for audio songs. I want to play a song only for just 30 seconds. After that, player must be closed. It should be start again, if I press START button again.

This is the code for creating a media player:

    MediaPlayer mediaPlayer = new MediaPlayer();
    public void songPreview(String songURL){

        try{
        mediaPlayer=new MediaPlayer();
        mediaPlayer.setDataSource(songURL);
             mediaPlayer.prepare();
             mediaPlayer.start();

        } catch(Exception ex){
            ex.printStackTrace();
        }
    }

Please suggest me what code should I use to play my song only for 30 seconds after that it will stop, and if I want to play again then I have to press start button.

Note: Please provide me logic to stop media player after 30 second.

Thank you in advance.

like image 598
Pushpendra Kuntal Avatar asked Sep 12 '11 05:09

Pushpendra Kuntal


1 Answers

use countdownTimer to complete your goal in which you can set countdown timer till 30 second manually. when countdown finish process it will go to finish method and execute finish method code ::

    CountDownTimer cntr_aCounter = new CountDownTimer(3000, 1000) {
        public void onTick(long millisUntilFinished) {

            mp_xmPlayer2.start();
        }

        public void onFinish() {
            //code fire after finish
               mp_xmPlayer2.stop();
        }
        };cntr_aCounter.start();
like image 81
Nikunj Patel Avatar answered Nov 02 '22 23:11

Nikunj Patel