Event for Videoview Playback State or Mediacontroller Play/Pause

Event for VideoView playback state or MediaController play/pause

If you're using the MediaController in combination with a VideoView, it should be relatively easy to extend the latter and add your own listener to it.

The custom VideoView would then look something like this in its most basic form:

public class CustomVideoView extends VideoView {

private PlayPauseListener mListener;

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

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

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

public void setPlayPauseListener(PlayPauseListener listener) {
mListener = listener;
}

@Override
public void pause() {
super.pause();
if (mListener != null) {
mListener.onPause();
}
}

@Override
public void start() {
super.start();
if (mListener != null) {
mListener.onPlay();
}
}

public static interface PlayPauseListener {
void onPlay();
void onPause();
}

}

Using it is identical to using a regular VideoView, with the only difference being that we can now hook up our own listener to it.

// Some other code above...
CustomVideoView cVideoView = (CustomVideoView) findViewById(R.id.custom_videoview);
cVideoView.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {

@Override
public void onPlay() {
System.out.println("Play!");
}

@Override
public void onPause() {
System.out.println("Pause!");
}
});

cVideoView.setMediaController(new MediaController(this));
cVideoView.setVideoURI(...);
// or
cVideoView.setVideoPath(...);
// Some other code below...

Finally, you may also declare it in your xml layout and inflate it (as shown above) - just make sure your use <package_name>.CustomVideoView. Example:

<mh.so.CustomVideoView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/custom_videoview" />

How to catch event when click pause/play button on MediaController

A bit of a late answer, but I posted a solution to an identical question overhere, in case you're still looking (or others that perhaps bump into this...)

//Edit: ups, didn't notice someone already posted a link as comment...

Android mediacontroller Play Pause controls not refresh properly

I've been having the same issue. I was not calling MediaController.setVideoView as you were, as I thought VideoView.setMediaController was sufficient for wiring things up. I tried adding that, then moving the call to show within onPrepared, and now it is working.

I wish I had a better understanding; my best guess is that perhaps everything needs to be wired up properly before the media is prepared, and before calling show. In any case, here is what I have:

mMediaController = new MediaController(VideoPlayerActivity.this, false);

mVideoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer pMp) {
mMediaController.show();
}
});

mVideoView.setMediaController(mMediaController);
mMediaController.setMediaPlayer(mVideoView);
mVideoView.setVideoPath(uri); // may not be applicable in your case
mVideoView.requestFocus();
mVideoView.start();

VideoView onTouch events: pause/resume video, and show/hide MediaController and ActionBar

To first show the video as paused, simply change seekTo(0) to seekTo(1) in your code. This will move the video to the time at 1 millisecond and you can take it from there.

//edited here
private int position = 1;
private MediaController mMediaController;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mMediaController = new MediaController(getActivity());
...
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{

if (savedInstanceState != null)
{
position = savedInstanceState.getInt("position");
}

View v = inflater.inflate(R.layout.fragment_video_view, parent, false);

mVideoView = (VideoView) v.findViewById(R.id.fragmentVideoView);
mVideoView.setVideoPath(videoPath);
mVideoView.setMediaController(mMediaController);

mVideoView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent motionEvent)
{
if (mVideoView.isPlaying())
{
mVideoView.pause();
if (!getActivity().getActionBar().isShowing())
{
getActivity().getActionBar().show();
mMediaController.show(0);
}
position = mVideoView.getCurrentPosition();
return false;
}
else
{
if (getActivity().getActionBar().isShowing())
{
getActivity().getActionBar().hide();
mMediaController.hide();
}
mVideoView.seekTo(position);
mVideoView.start();
return false;
}
}
});

mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mediaPlayer)
{
//here
mVideoView.seekTo(1);
}
});
//here
if (position != 1)
{
mVideoView.seekTo(position);
mVideoView.start();
}
else
{
//here
mVideoView.seekTo(1);
}
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);

if (mVideoView != null)
{
savedInstanceState.putInt("position", mVideoView.getCurrentPosition());
}

mVideoView.pause();
}

}

How to detect when VideoView starts playing (Android)?

I ended up using VideoView.setOnPreparedListener. This was enough to cover my problem (play button drawable change to pause)



Related Topics



Leave a reply



Submit