GSYVideoPlayer实现视频播放

Posted zhangjin1120

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GSYVideoPlayer实现视频播放相关的知识,希望对你有一定的参考价值。

效果图

demo已上传到coding,欢迎clone,https://e.coding.net/zhangjinhome/demo/GsyVideoPlayerDemo.git

  • 导入gsy库:

github仓库地址:https://github.com/CarGuo/GSYVideoPlayer

增加jitpack和阿里云代理地址:

allprojects 
    repositories 
			 ...
        maven  url 'https://jitpack.io' 
        maven  url "https://maven.aliyun.com/repository/public" 
    

添加gsy依赖:

    implementation 'com.github.CarGuo.GSYVideoPlayer:GSYVideoPlayer:v8.1.7-release-jitpack'
  • 辅助操作

  • minSdk调至19: minSdkVersion 19
  • 添加网络权限:
    <uses-permission android:name="android.permission.INTERNET" />
  • 添加multidex(详见Demo源码)
  • 设置横屏
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
            android:screenOrientation="landscape"
  • 引入并使用

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_play"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
        android:id="@+id/video_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

</RelativeLayout>

MainActivity.java代码:

public class MainActivity extends AppCompatActivity 
    StandardGSYVideoPlayer videoPlayer;

    OrientationUtils orientationUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (getActionBar() != null) 
            getActionBar().hide();
        
        if (getSupportActionBar() != null) 
            getSupportActionBar().hide();
        
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        init();
    

    private void init() 
        videoPlayer =  (StandardGSYVideoPlayer)findViewById(R.id.video_player);

        String source1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
        videoPlayer.setUp(source1, true, "测试视频");

        //增加封面
//        ImageView imageView = new ImageView(this);
//        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//        imageView.setImageResource(R.mipmap.xxx1);
//        videoPlayer.setThumbImageView(imageView);
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils(this, videoPlayer);
        //设置全屏按键功能,这是使用的是选择屏幕,而不是全屏
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                // ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
                // 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
                //orientationUtils.resolveByClick();
                finish();
            
        );
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true);
        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                onBackPressed();
            
        );


        ///不需要屏幕旋转
        videoPlayer.setNeedOrientationUtils(false);

        videoPlayer.startPlayLogic();
    


    @Override
    protected void onPause() 
        super.onPause();
        videoPlayer.onVideoPause();
    

    @Override
    protected void onResume() 
        super.onResume();
        videoPlayer.onVideoResume();
    

    @Override
    protected void onDestroy() 
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    

    @Override
    public void onBackPressed() 
///       不需要回归竖屏
//        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) 
//            videoPlayer.getFullscreenButton().performClick();
//            return;
//        
        //释放所有
        videoPlayer.setVideoAllCallBack(null);
        super.onBackPressed();
    

以上是关于GSYVideoPlayer实现视频播放的主要内容,如果未能解决你的问题,请参考以下文章

Android GSYVideoPlayer视频播放器

Android GSYVideoPlayer自定义封面

开源播放器,音视频播放相关调研

探索移动端音视频与GSYVideoPlayer之旅 | Agora Talk

Android动态的设置5图或1视频,并播放或浏览图片

Android投屏方案(基于cling)