Originally, I had a MediaPlayer mPlayers to play/pause/stop my Audio file. Now, I'm trying to add a seek bar for my media. I tried using Threads and this is what I came up with:
In onCreate() :
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
mPlayer.seekTo(progress);
}
////........Omitted Code..........\\\\
//At the very end of the method:
seekThread = new Thread(this); //My Activity implements:Runnable
seekThread.start();
Then, I implemented a method run()
to update my seek bar every 1 sec:
public void run() {
try {
while(mPlayer != null){
int currentPosition = mPlayer.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
threadHandler.sendMessage(msg);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Handler threadHandler = new Handler(){
public void handleMessage(Message msg){
seekBar.setProgress(msg.what);
}
};
However, In my HTC Magic, the audio run inefficiently. Some words are repeated. It seems like the seek bar is going back a little bit every some time.
To make you understand the problem well, Let's say this: if the audio says that: abcdefghijklmn , then it will be like this: abccdeffghijjklmnn.
Any improvement you suggest to my thread ? Am I doing it right?
Not sure this will help, but try:
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
mPlayer.seekTo(progress);
}
}
Without this condition, mPlayer.seekTo(progress); is called every time you call seekBar.setProgress(msg.what);