Android

MediaController简介

2022-06-29  本文已影响0人  Nothing_655f

MediaController简介

官方API 文档

MediaController | Android Developers

使用demo

Android自带的MediaController实现网络播放和本地播放

结合VideoView和MediaController的例子简单介绍下

base/core/java/android/widget/VideoView.java

public class VideoView extends SurfaceView
        implements MediaPlayerControl, SubtitleController.Anchor {

     // ....
    // MediaPlayerControl 接口类方法实现
    @Override
    public void start() {
        if (isInPlaybackState()) {
            mMediaPlayer.start();
            mCurrentState = STATE_PLAYING;
        }
        mTargetState = STATE_PLAYING;
    }
    public void setMediaController(MediaController controller) {
        if (mMediaController != null) {
            mMediaController.hide();
        }
        mMediaController = controller;
        attachMediaController();
    }

    private void attachMediaController() {
        if (mMediaPlayer != null && mMediaController != null) {
            mMediaController.setMediaPlayer(this);
            View anchorView = this.getParent() instanceof View ?
                    (View)this.getParent() : this;
            mMediaController.setAnchorView(anchorView);
            mMediaController.setEnabled(isInPlaybackState());
        }
    }
}

可以看到VideoView 其继承是SurfaceView 和实现了MediaPlayerControl, SubtitleController.Anchor 接口

实现 MediaPlayerControl 接口主要是将 VideoView class this 指针传递给,所以 MediaController 中的事件监听,比如上下片源切换,快进快退就可以通过MediaPlayerControl 接口类实现回调到 VideoView 中,这样子就操作到具体对应的MediaPlayer 方法

base/core/java/android/widget/MediaController.java

    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();
    }
上一篇 下一篇

猜你喜欢

热点阅读