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" />