如果你在组合使用MediaController
用VideoView
,它应该是相对容易扩展后者并添加自己的侦听器。然后
定制VideoView会是这个样子在其最基本的形式:
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();
}
}
使用它等同于使用常规VideoView
,唯一的区别是,我们现在可以连接我们自己的监听器到它。
// 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...
最后,您还可以声明它在你的XML布局,它充气(如上图所示) - 只要确保你使用<package_name>.CustomVideoView
。例如:
<mh.so.CustomVideoView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/custom_videoview" />
天才!谢谢你,先生,这工作完美。 +50给你。我不能等到我知道Java以及我知道其他事情如动作脚本。 – Ronnie
很高兴有帮助。 :)顺便说一下,调用'super'通常与重写方法一起完成。通过重写,你基本上重新定义了基类/超类/父类的方法。如果你用这种重写的方法调用'super',你会保留它的功能,如果你的目标是*添加*功能,这就是你想要做的。有时候你可能想要*替换*功能,在这种情况下,你可以通过不调用'super'来获得期望的结果。 –
好吧,这是我认为和非常有用的知道,谢谢 – Ronnie