Question

I have a SeekBar, it displays correctly MediaPlayer progress. However, I have troubles with seeking - if I seek scroll box somewhere it just returns on the position where audio file is playing.

public class EntityPageActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
    private SeekBar seekBar;
    private Button startMedia;
    private Button pauseMedia;
    private MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entity_page);             

        AudioControl();         

    }

    public void AudioControl(){
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        startMedia = (Button) findViewById(R.id.play);
        pauseMedia = (Button) findViewById(R.id.pause);
        startMedia.setOnClickListener(this);
        pauseMedia.setOnClickListener(this); 
    }

    public void run() {
        int currentPosition= 0;
        int total = mp.getDuration();
        while (mp!=null && currentPosition<total) {
            try {
                Thread.sleep(1000);
                currentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            seekBar.setProgress(currentPosition);
        }
    }

    public void onClick(View v) {
        if (v.equals(startMedia)) {
            if (mp != null && mp.isPlaying()) return;
            if(seekBar.getProgress() > 0) {
                mp.start();
                return;
            }
            mp = MediaPlayer.create(EntityPageActivity.this, R.raw.gl1);
            mp.start();                     
            seekBar.setProgress(0);
            seekBar.setMax(mp.getDuration());
            new Thread(this).start();
        }

        if (v.equals(pauseMedia) && mp!=null) {
            mp.pause();
        }       

    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        if(fromUser) mp.seekTo(progress);

    }
}

Thus, onProgressChanged isn't enough to allow SeekBar to control MediaPlayer?

What should I add/correct to allow seeking?

Thanks

UPDATE

I found out that onProgressChanged is never invoked by some reason. However I can't figure out why

Was it helpful?

Solution

The problem is seekBar.setOnSeekBarChangeListener(this); So put it in Your AudioControl() Method..... You dont set oncheckchangedlistner to your seekbar....

OTHER TIPS

Try with the following code:

public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        if(fromUser){
        mp.seekTo(progress);
        seekbar.setProgress(progress);}
 }

You have to include the seekbar methods inside the onCreate. I had the same problem, and it worked after that.

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
if(fromUser){ 
    mediaPlayer.seekTo(progress); 
    seekBar.setProgress(progress);
}

Try this code in onCreate() method and run the project.I hope this is working.

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            mediaPlayer.seekTo(i);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

I am using this simple code to do the trick.

package in.bhartisoftwares.amit.allamitapps;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class audioControllerTwo extends AppCompatActivity {
    MediaPlayer mPlayer;        
    SeekBar audioTraverse;

here I have defined My SeekBar and MediaPlayer in public class. Then on OnCreate Method I have code like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio_controller_two);

    mPlayer = MediaPlayer.create(this, R.raw.song);

Here I am creating mPlayer variable, that finds my audio file called "song" which is present in "raw" folder.

Then I grabbed my SeekBar called "audioTraverse" and assigned it to "audioTraverse" variable and grabbing the "Duration" of audio file and assigning that duration to my "audioTraverse" SeekBar as follows:

    audioTraverse = (SeekBar) findViewById(R.id.audioTraverse);
    audioTraverse.setMax(mPlayer.getDuration());

Now I am running a timer that will run after every second and it will update the SeekBar position accordingly. See the code below:

new Timer().scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        audioTraverse.setProgress(mPlayer.getCurrentPosition());
    }
},0, 1000); // here 0 represents first time this timer will run... In this case it will run after 0 seconds after activity is launched
// And 1000 represents the time interval afer which  this thread i.e., "run()" will execute. In this case, it will execute after every 1 second

Now I am adding "SeekBarChangeListener", this will look for any changes on SeekBar and in "OnProgressChange()" method I am using ".seekTo()" method to go to a particular duration in audio file. Code is as follows:

        audioTraverse.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                Log.i("Tranverse Change: ", Integer.toString(i));
                mPlayer.seekTo(i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

if you want to resume media player to specific position from seekbar change progress, I recommend you to do that in onStopTrackingTouch() method. that method will notify that the user has finished a touch gesture, then set media player position from seekbar's current position.

 @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    mediaPlayer.seekTo(seekBar.getProgress());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top