Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: onSeekCompleteListener with VideoView

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

like image 311
Farhan Avatar asked Oct 26 '11 04:10

Farhan


3 Answers

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

    }
});
like image 167
Matthieu Coisne Avatar answered Nov 15 '22 09:11

Matthieu Coisne


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

}

like image 29
Tal Kanel Avatar answered Nov 15 '22 10:11

Tal Kanel


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.

like image 41
Peter Tran Avatar answered Nov 15 '22 10:11

Peter Tran