0

I have mediaPlayer for playing sound in android and we want play files based on start time and finishing time for example :

start 10370 ,end: 14759 [OR] start 4754 ,end: 7836

i used this code for my problem but it's not working perfect Pause/Stop MediaPlayer Android at given time programmatically

My code :

mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.seekTo(words.getW_start());
mediaPlayer.start();
handler.postDelayed(stopPlayerTask, words.getW_end());

Do you Know why code for these times doesn't work?

Community
  • 1
  • 1
sr_ farzad
  • 45
  • 1
  • 9

1 Answers1

0

I could resolve problem .complete code:

mediaPlayer.setDataSource(fd);
mediaPlayer.prepare();
mediaPlayer.seekTo(words.getW_start());
mediaPlayer.start();
new CountDownTimer(endTime, 10) {
 public void onTick(long millisUntilFinished) {

 if(MediaPlayerUtility.getTime(mediaPlayer)>=endTime){
      mediaPlayer.stop();
    }

         }

    public void onFinish() {


                            }
     }.start();

and MediaPlayerUtility class:

public class MediaPlayerUtility {


    public static long getTime(MediaPlayer mediaPlayer) {


        long totalDuration = mediaPlayer.getDuration(); // to get total duration in milliseconds

        long currentDuration = mediaPlayer.getCurrentPosition(); // to Gets the current playback position in milliseconds

        return currentDuration;

    }


}
sr_ farzad
  • 45
  • 1
  • 9