Reputation: 93561
I need to detect when a VideoView is paused, so I can hide some UI elements on my screen. VideoView does not have a mechanism to inform you of a pause event. How do I do this?
Upvotes: 1
Views: 1871
Reputation: 93561
I haven't seen a good answer to this (so many people using threads). Here's mine:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;
public class PlayStateBroadcastingVideoView extends VideoView{
public interface PlayPauseListener {
void onPlay();
void onPause();
}
private PlayPauseListener mListener;
public PlayStateBroadcastingVideoView(Context context) {
super(context);
}
public PlayStateBroadcastingVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PlayStateBroadcastingVideoView(Context context, AttributeSet attrs, int theme) {
super(context, attrs, theme);
}
@Override
public void pause() {
super.pause();
if(mListener != null) {
mListener.onPause();
}
}
@Override
public void start() {
super.start();
if(mListener != null) {
mListener.onPlay();
}
}
public void setPlayPauseListener(PlayPauseListener listener) {
mListener = listener;
}
}
It works because upon diving into the code (at least as of 5.0), the only function that puts it into the pause state is pause, and the only one that puts it into the playing state is start. So we simply hook these to notify us via a listener. Then use this class in your layout in place of VideoView.
Upvotes: 2