欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发之音视频(二)

程序员文章站 2022-04-26 15:01:55
我且把视频开发大致分为视频显示控件和音视频控制控件、弹窗控件 MediaPlayer MediaController Popu 播放器MediaPlayer可以理解为一...

我且把视频开发大致分为视频显示控件和音视频控制控件、弹窗控件

MediaPlayer

MediaController

Popu


播放器MediaPlayer可以理解为一个容器,容纳多种布局,这里布局按照16:9的比例显示画面,摆在博主面前的第一个难题出现了,如何实现小屏和全屏的无缝切换?网上有诸多方案并不能令博主满意,最后在几经尝试以及一位博友提示下想到了一个方案

修改MediaPlayer onMeasure函数,根据不同的模式动态requestLayout 动态调整控件宽高,同时回调函数通知隐藏状态栏和标题栏(不一定是标题栏,所以回调函数自己控制)

Tip:
一定要注意修改manifest文件 android:configChanges="screenSize|keyboardHidden|orientation",这样横竖屏切换不会走生命周期,只会changed回调

下面是伪类代码块

package idea.analyzesystem.ijkplayer.widget;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import idea.analyzesystem.ijkplayer.interfaces.MediaPlayerListener;

/**
 * Created by idea on 2018/2/11.
 */
public class MediaPlayer extends FrameLayout implements View.OnClickListener{

    private final Point screenPoint;

    public static final int MODE_LANDSCAPE = 0;

    public static final int MODE_PORTRAITSCREEN = 1;

    private int mode = MODE_PORTRAITSCREEN;

    public int getMode() {
        return mode;
    }

    /**
     *  MODE_LANDSCAPE 、MODE_PORTRAITSCREEN
     * @param mode
     */
    public void setMode(int mode) {
        if(this.mode!=mode){
            this.mode = mode;
            switch (getMode()){
                case MODE_LANDSCAPE:
                    if(getScreenChangedListener()!=null){
                        getScreenChangedListener().onLandScape();
                    }
                    requestLayout();
                    break;

                case MODE_PORTRAITSCREEN:
                    if(getScreenChangedListener()!=null){
                        getScreenChangedListener().onPortailScreen();
                    }
                    requestLayout();
                    break;
            }
        }

    }

    public MediaPlayer(Context context) {
        this(context,null,0);
    }

    public MediaPlayer(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MediaPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        screenPoint = new Point();
        windowManager.getDefaultDisplay().getSize(screenPoint);

        setOnClickListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        switch (getMode()){
            case MODE_LANDSCAPE:
                setMeasuredDimension(screenPoint.y,screenPoint.x);
                break;

            case MODE_PORTRAITSCREEN:

                setMeasuredDimension(screenPoint.x,screenPoint.x/16*9);
                break;
        }
    }


    @Override
    public void onClick(View v) {
    }

    /************************************Listener*********************************/

    private MediaPlayerListener.ScreenChangedListener screenChangedListener;

    public MediaPlayerListener.ScreenChangedListener getScreenChangedListener() {
        return screenChangedListener;
    }

    public void setScreenChangedListener(MediaPlayerListener.ScreenChangedListener screenChangedListener) {
        this.screenChangedListener = screenChangedListener;
    }
}

播放界面伪代码块

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ideaMediaPlayer.setScreenChangedListener(this);
}

@Override
public void onLandScape() {
        titleView.setVisibility(View.GONE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

@Override
public void onPortailScreen() {
        titleView.setVisibility(View.VISIBLE);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

第一个知识点,如何实现小屏、全屏无缝切换