VideoView MediaPlayer的暂停播放监听播放时长的计时显示

VideoView MediaPlayer的暂停播放监听播放时长的计时显示

最近在做一个有关视频播放的项目时,要实现一个功能:就是记录视频的额播放时长,并当视频停止播放时,计时器也暂停纪录。一开始以为实现这个功能挺简单的,但是后来发现VideoView和mediaplayer没有暂停播放的监听发方法。

MediaPlayerController的官方接口如下:

public interface MediaPlayerControl {
        void    start();
        void    pause();
        int     getDuration();
        int     getCurrentPosition();
        void    seekTo(int pos);
        boolean isPlaying();
        int     getBufferPercentage();
        boolean canPause();
        boolean canSeekBackward();
        boolean canSeekForward();

        /**
         * Get the audio session id for the player used by this VideoView. This can be used to
         * apply audio effects to the audio track of a video.
         * @return The audio session, or 0 if there was an error.
         */
        int     getAudioSessionId();
    }

点击并拖拽以移动

从上面找不到暂停播放的相关监听接口。为了实现这个功能在网上找了一下资料,有人通过重写VideoView这个类来实现暂停播放监听,感觉这个方法挺好用的,记录一下为以后遇到类似的问题作参考。

首先新建一个 CustomVideoView 类继承VideoView类:

package com.mryzj.videotest;

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();
    }

}

点击并拖拽以移动

然后在Layout布局里加入CustomVideoView:

<com.mryzj.videotest.CustomVideoView
        android:id="@+id/videoview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"/>

点击并拖拽以移动

接着在Activity类中从布局取得VideoView:

CustomVideoView video = findViewById(R.id.videoview);

点击并拖拽以移动

最后就是实现监听的逻辑了:

video.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {

   public void onPlay() {
        //添加要实现监听事件的逻辑
        Log.d("playing","video is playing");
   }

    public void onPause() {
        //添加要实现监听事件的逻辑
        Log.d("paused","video is paused");
    }
});

点击并拖拽以移动

计时功能的实现逻辑如下:

private int timer;
private int hour = 0;
private int minute = 0;
private int second = 0; 
private int day = 0;
private PlayTimer thread;

private class PlayTimer extends Thread {
        public boolean stop;
        public void run() {
            while (!stop) {
                if (timer < 60) {
                    second = timer;
                }
                if (timer > 59) {
                    minute = timer / 60;
                    second = timer % 60;
                }
                if (minute > 59) {
                    hour = minute / 60;
                    minute = minute % 60;
                }
                if (hour > 23) {
                    day = hour / 24;
                    hour = hour % 24;
                }
                //playTime是一个TextView
                playTime.setText("已播放时长:" + day + "天" + hour + "时" + minute + "分" + second + "秒");
                try {
                    Thread.sleep(1000);
                    timer += 1;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 开始计时
     */
    private void start() {
        if (thread == null) {
            thread = new PlayTimer();
            thread.start();
        }
    }

    /**
     * 暂停计时
     */
    private void stop() {
        if (thread != null) {
            thread.stop = true;
            thread = null;
        }
    }

点击并拖拽以移动

结合上述的监听实现方法就可以实现播放时开始记录时长,暂停时就记录暂停:

video.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {
            public void onPlay() {
                start();
            }
            public void onPause() {
                stop();
            }
        });

点击并拖拽以移动

如有描述不当,欢迎指正!


评论区

 本篇
VideoView MediaPlayer的暂停播放监听播放时长的计时显示 VideoView MediaPlayer的暂停播放监听播放时长的计时显示
最近在做一个有关视频播放的项目时,要实现一个功能:就是记录视频的额播放时长,并当视频停止播放时,计时器也暂停纪录。一开始以为实现这个功能挺简单的,但是后来发现VideoView和mediaplayer没有暂停播放的监听发方法。
2019-10-23
下一篇 
【每日分享】慕课800G视频分享 【每日分享】慕课800G视频分享
分享800G的学习视频资源,有想要的资源可以拿走不谢!希望能帮到大家,助人为乐!
2019-10-23
  目录