0

I am working on a MediaPlayer. Player is working fine and music is being streamed fine but I am having trouble in setting the seekbar to work. I am assuming that seekbar is directly related to getting correct duration through getDuration and then we move forward.

Basically, I am following this tutorial and trying to modify it to work if data source is a url rather than a local file.

Here is how I am setting the mediaPlayer object and trying to getDuration

Uri uri = Uri.parse(sURL);
System.out.println(uri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(this, uri); 
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {  
     @Override  
     public void onPrepared(MediaPlayer mp) {  
         seekBar.setMax(mp.getDuration());
         System.out.println("Duration of file is = " + mp.getDuration());
     }  
});
initViews(mediaPlayer);

MediaPlayer object was initiated in onCreate. These are the rest of the methods dealing with seekbar and progress tracking.

// This method set the setOnClickListener and method for it (buttonClick())
private void initViews(MediaPlayer mediaPlayer) {
    Log.i("Main", "initViews");
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {buttonClick();}});

    seekBar = (SeekBar) findViewById(R.id.SeekBar01);       
    //Log.i ("info", String.valueOf(mediaPlayer.getDuration()));
    seekBar.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
        seekChange(v);
        return false; }
    });

}

public void startPlayProgressUpdater() {
    seekBar.setProgress(mediaPlayer.getCurrentPosition());

    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();
            }
        };
        handler.postDelayed(notification,1000);
    }else{
        mediaPlayer.pause();
        buttonPlayStop.setText(getString(R.string.play_str));
        seekBar.setProgress(0);
    }
} 

// This is event handler thumb moving event
private void seekChange(View v){
    if(mediaPlayer.isPlaying()){
        SeekBar sb = (SeekBar)v;
        mediaPlayer.seekTo(sb.getProgress());
    }
}

// This is event handler for buttonClick event
private void buttonClick(){
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
        buttonPlayStop.setText(getString(R.string.pause_str));
        try{
            mediaPlayer.start();
            startPlayProgressUpdater(); 
        }catch (IllegalStateException e) {
            mediaPlayer.pause();
        }
    }else {
        buttonPlayStop.setText(getString(R.string.play_str));
        mediaPlayer.pause();
    }
}

Can anyone please guide me in this regard?

sandrstar
  • 12,503
  • 8
  • 58
  • 65
Hammad Tariq
  • 1,523
  • 3
  • 14
  • 29
  • It depends on your content - I think it works with progressive download but not live streaming. How would you seek a live football match for example? Are you sure your source supports seeking? What is the "duration" of the bbc news live stream? Infinite! :) – Ken Wolf Jul 15 '13 at 12:25
  • These are Mp3 files getting fetched from a server, I uploaded those files, no live streaming involved. – Hammad Tariq Jul 15 '13 at 20:33
  • try this https://stackoverflow.com/a/65978856/11174675 – Magic due to Logic Jan 31 '21 at 12:12

0 Answers0