PlayActivity.java 4.91 KB
/*
 * Copyright (c) Wondertek Technologies Co., Ltd. 2019-2022. All rights reserved.
 */

package com.wd.player;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.aliyun.player.source.UrlSource;
import com.aliyun.svideo.wd.player.R;
import com.wd.player.playerutil.ScreenUtils;
import com.wd.player.widget.VideoAndLivePlayerView;

/**
 * 描述:播放器组件待完善
 *
 * @author : lvjinhui
 * @since: 2022/7/1
 */
public class PlayActivity extends AppCompatActivity {
    /**
     * 播放器视图
     */
    private VideoAndLivePlayerView aliyunRenderView;
    private boolean isInit = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        aliyunRenderView = findViewById(R.id.playerView);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    private void initPlayer() {
        UrlSource urlSource = new UrlSource();
//        urlSource.setUri(PlayParameter.PLAY_PARAM_URL);
        initDataSource(urlSource);
    }


    private void initDataSource(UrlSource urlSource) {
        if (urlSource == null) {
            return;
        }
        aliyunRenderView.setLocalSource(urlSource);
    }

    /**
     * 播放器
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (aliyunRenderView != null) {
            boolean handler = aliyunRenderView.onKeyDown(keyCode, event);
            if (!handler) {
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isInit){
            initPlayer();
        }
        isInit = true;

        updatePlayerViewMode();
        if (aliyunRenderView != null) {
            aliyunRenderView.setAutoPlay(true);
            aliyunRenderView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (aliyunRenderView != null) {
            aliyunRenderView.setAutoPlay(false);
            aliyunRenderView.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (aliyunRenderView != null) {
            aliyunRenderView.onDestroy();
            aliyunRenderView = null;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        updatePlayerViewMode();
    }

    private void updatePlayerViewMode() {
        if (aliyunRenderView != null) {
            int orientation = getResources().getConfiguration().orientation;

            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                aliyunRenderView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                // 设置view的布局,宽高之类
                RelativeLayout.LayoutParams aliVcVideoViewLayoutParams =
                        (RelativeLayout.LayoutParams) aliyunRenderView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = (int) (ScreenUtils.getWidth(this) * 9.0f / 16);
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                // 转到横屏了。
                // 隐藏状态栏
//                if (!ToolsUtil.isStrangePhone()) {
//                    this.getWindow()
//                            .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//                    aliyunRenderView.setSystemUiVisibility(
//                            View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                                    | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//                }
                // 设置view的布局,宽高
                RelativeLayout.LayoutParams aliVcVideoViewLayoutParams =
                        (RelativeLayout.LayoutParams) aliyunRenderView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
        }
    }
}