VideoView 开始暂停监听

现在做个视频播放器,当暂停的时候要在界面上弹出一张图片,找了半天,发现VideoView没有暂停播放的监听发方法,从网上找了下资料,需要重写VideoView这个类

以下代码是网上的

 首先写一个CustomVideoView类:package our.english.listening;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.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();
        }
    }

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

}
在Activity类里从XML取得VideoView:CustomVideoView video = (CustomVideoView)findViewById(R.id.videoview);
注意XML中的VideoView应该这样写:       
 
ok啦~
用法如下: video.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {

   public void onPlay() {
        System.out.println("video is playing");
   }

public void onPause() {
// TODO Auto-generated method stub
System.out.println("video is paused");
}
});


你可能感兴趣的:(Android)