i am using VideoView and seek bar but when i seekTo(..) on desired position through seekBar it play video from starting.
i trying this code:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { mVideoView.seekTo(progress); }
As Reno mentioned, you have to wait for the seeking to complete.
VideoView
does not have a OnSeekCompleteListener()
but you can access the MediaPlayer
from the onPrepared
method of the VideoView
and then set the OnSeekCompleteListener
, like this :
mVideoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mp) {
//TODO: Your code here
}
});
}
});
The call to VideoView.seekTo()
is a wrapper around MediaPlayer.seekTo()
. This function returns almost immediately even though the actual seeking is still being performed. Therefore you want to wait for seeking to complete via MediaPlayer.OnSeekCompleteListener
.
However, as Reno mentioned, the standard VideoView
does not support OnSeekCompleteListener
.
But you can copy and locally customize the VideoView
class to add this support yourself.
First, start with a copy of VideoView.java. Or you can clone the entire frameworks/base repo but warning it is over 1 gig of data to download.
Copy VideoView.java into your Eclipse Android project and it will start building but fail. Here's what I did to get it to compile:
MetaData
. The fix for this is on my todo list. These need to be replaced with calls to MediaMetadataRetriever
.mContext
with calls to getBaseContext()
Now you are ready to add the code for OnSeekCompleteListener
. The implementation is similar to the other listeners, i.e OnCompletionListener
.
public class VideoView extends SurfaceView
implements MediaPlayerControl {
// The client's listener which is the notification callback.
private OnSeekCompleteListener mOnSeekCompleteListener;
// Set up MediaPlayer to forward notifications to client.
private MediaPlayer.OnSeekCompleteListener mSeekCompleteListener =
new MediaPlayer.OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
if (mOnCompletionListener != null) {
mOnCompletionListener.onCompletion(mMediaPlayer);
}
}
};
// API for client to set their listener.
public void setOnSeekCompleteListener(OnSeekCompleteListener l)
{
mOnSeekCompleteListener = l;
}
}
Finally, update your own code:
android.widget.VideoView
to use your customized VideoView
.setOnSeekCompleteListener()
.Your code now receives notifications when the seek has really completed and it can then perform subsequent seeks.
You have to wait for the seeking to complete, unfortunately VideoView
does not have a OnSeekCompleteListener()
(why Google? -_-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With