6

I am using VideoView to play video files. I am using seekTo function in order to play the video from where it has been left off. However, I wanted to do some operations when the seek operation is finished. For that I need to use onSeekCompleteListener; however, onSeekCompleteListener is not supported with VideoView; it can be used with MediaPlayer. My Question is that "is there any way by which I can use onSeekCompleteListener" with VideoView?

Thanks alot

Farhan
  • 3,206
  • 14
  • 49
  • 62
  • I have the same problem, if you have solution to your question - I'll be glad to here what is the answer.. – Tal Kanel Jan 25 '12 at 16:31
  • unfortunately, onSeekCompleteListener can't be used with VideoView. I also don't understand why this is not supported for VideoView if it supports seekTo operation. The solution here is to use MediaPlayer instead of VideoView since onSeekCompleteListener is available for MediaPlayer. You can get the same functionality using MediaPlayer instead of VideoView. Hope this helps you.. – Farhan Feb 01 '12 at 02:17
  • thanks for your answer, but - I have to say I found much better and simpler solution as I'm writing as answer to your original question. Have a look at my custom class which extends VideoView – Tal Kanel Feb 01 '12 at 21:34
  • please mark it as solution to your question. I really think my solution is good for your original question – Tal Kanel Feb 01 '12 at 21:43
  • Tal: your new API onTimeBarSeekChanged is semantically different from onSeekCompleteListener.onSeekComplete and therefore may not meet Farhan's requirement. The seekTo functions initiates a low-level seek and onSeekComplete is intended to notify client when the low-level seek is completed. onTimeBarSeekChanged only notifies that seekTo has been called not completed. – Peter Tran Aug 13 '12 at 16:40

3 Answers3

19

There is no need to create a custom VideoView.

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
            }
        });

    }
});
Matthieu Coisne
  • 624
  • 1
  • 13
  • 25
10
public class ObservableVideoView extends VideoView
{

private IVideoViewActionListener mVideoViewListener;
private boolean mIsOnPauseMode = false;

public interface IVideoViewActionListener
{
    void onPause();
    void onResume();
    void onTimeBarSeekChanged(int currentTime);
}

public void setVideoViewListener(IVideoViewActionListener listener)
{
    mVideoViewListener = listener;
}

@Override
public void pause()
{
    super.pause();

    if (mVideoViewListener != null)
    {
        mVideoViewListener.onPause();
    }

    mIsOnPauseMode = true;
}

@Override
public void start()
{
    super.start();

    if (mIsOnPauseMode)
    {
        if (mVideoViewListener != null)
        {
            mVideoViewListener.onResume();
        }

        mIsOnPauseMode = false;
    }
}

@Override
public void seekTo(int msec)
{
    super.seekTo(msec);

    if (mVideoViewListener != null)
    {
        mVideoViewListener.onTimeBarSeekChanged(msec);
    }
}

public ObservableVideoView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ObservableVideoView(Context context)
{
    super(context);
}

public ObservableVideoView(Context context, AttributeSet attrs, int defStyle) 
{
    super(context, attrs, defStyle);
}

}

you can listen for events like this:

public class VideoPlayerActivity extends Activity
{
public static final String VIDEO_URL = "VideoUrl";
private String path = "";
private ObservableVideoView mVideoView;

@Override
public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.video_player_activity_layout);
    mVideoView = (ObservableVideoView) findViewById(R.id.videoView1);
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.setVideoViewListener(mVideoViewListener);


    path = getIntent().getStringExtra(VIDEO_URL);       
    if (path == "")
    {
        Toast.makeText(this, "Please edit VideoViewDemo Activity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG)
                .show();
    }
    else
    {   
        mVideoView.setVideoPath(path);
        mVideoView.requestFocus();
        mVideoView.start();
    }
}

private IVideoViewActionListener mVideoViewListener = new IVideoViewActionListener()
{
    @Override
    public void onTimeBarSeekChanged(int currentTime)
    {
        //TODO what you want
    }

    @Override
    public void onResume()
    {
        //TODO what you want
    }

    @Override
    public void onPause()
    {
        //TODO what you want
    }
};

}

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
1

Farhan, you are correct, onSeekCompleteListener is not supported by VideoView.

But you can copy and locally customize the VideoView class to add this support yourself.

I show how to do this in my answer to 7990784.

Community
  • 1
  • 1
Peter Tran
  • 1,626
  • 1
  • 17
  • 26