Android多功能视频播放器GSYVideoPlayer开发流程
幸大叔 人气:0前言
今天,和大家分享一个开源的多功能视频播放器 — GSYVideoPlayer,支持弹幕,滤镜、水印、gif截图,片头广告,声音、亮度调节等众多功能,这里就利用它来实现一个标准的视频播放器,那么,话不多说,Go ~
GSYVideoPlayer 一个基于IJkPlayer的播放器
支持调节声音亮度
边播边缓存 使用了AndroidVideoCache;ExoPlayer使用SimpleCache
支持多种协议h263\4\5、Https、concat、rtsp、hls、rtmp、crypto、mpeg等等
简单滤镜(马赛克、黑白、色彩过滤、高斯、模糊、模糊等等20多种)、动画、(水印、画面多重播放等)
视频第一帧、视频帧截图功能,视频生成gif功能。
调整显示比例:默认、16:9、4:3、填充;播放时旋转画面角度(0,90,180,270);镜像旋转
IJKPlayer、EXOPlayer、MediaPlayer切换、自定义内核
小窗口、多窗体下(包括桌面)的小窗口播放。
片头广告、跳过广告支持、中间插入广告功能。
暂停前后台切换不黑屏;调整不同清晰度的支持;无缝切换支持;锁定/解锁全屏点击功能;进度条小窗口预览
可自定义渲染层、自定义管理层、自定义播放层(控制层)、自定义缓存层
引入依赖
maven { url 'https://jitpack.io' }
maven { url "https://maven.aliyun.com/repository/public" }
//完整版引入
implementation 'com.github.CarGuo.GSYVideoPlayer:GSYVideoPlayer:v8.3.3-release-jitpack'
//是否需要AliPlayer模式
implementation 'com.github.CarGuo.GSYVideoPlayer:GSYVideoPlayer-aliplay:v8.3.3-release-jitpack'
开发设置
设置 Activity 的 configChanges 和 screenOrientation
<activity android:name=".MainActivity" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:screenOrientation="fullSensor" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer android:id="@+id/videoPlayer" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
设置要播放的视频链接
videoPlayer = findViewById(R.id.videoPlayer) videoPlayer.setUp(videoUrl, true, "title")
如果你想添加视频封面的话,可以这样设置
val coverImg = ImageView(this) coverImg.setImageResource(R.drawable.cover) videoPlayer.thumbImageView = coverImg
具体实现
隐藏自带的标题和返回键
videoPlayer.titleTextView.visibility = View.GONE videoPlayer.backButton.visibility = View.GONE
设置旋转,横屏显示
orientationUtils = OrientationUtils(this, videoPlayer) videoPlayer.fullscreenButton.setOnClickListener { orientationUtils.resolveByClick() }
开始播放
videoPlayer.startPlayLogic()
除此之外,还需处理一下生命周期
override fun onPause() { super.onPause() videoPlayer.onVideoPause() } override fun onResume() { super.onResume() videoPlayer.onVideoResume() } override fun onDestroy() { super.onDestroy() GSYVideoManager.releaseAllVideos() orientationUtils.releaseListener() }
在横屏时,如果想点击返回键回到竖屏而不是退出的话,需要重写 onBackPressed
override fun onBackPressed() { if (orientationUtils.screenType == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { videoPlayer.fullscreenButton.performClick() return } videoPlayer.setVideoAllCallBack(null) super.onBackPressed() }
如果,视频是以列表的形式展现的话,也是OK的,首先搞个RecyclerView
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".VideoListActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/video_recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
编写item布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="30dp"> <com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer android:id="@+id/item_player" android:layout_width="match_parent" android:layout_height="300dp" /> </LinearLayout>
RecyclerView适配器
class VideoAdapter(private val urlList: List<String>, private val context: Context) : RecyclerView.Adapter<VideoAdapter.ViewHolder>() { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val itemPlayer: StandardGSYVideoPlayer = view.findViewById(R.id.item_player) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(context).inflate(R.layout.item_video, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { //配置StandardGSYVideoPlayer with(holder.itemPlayer) { setUpLazy(urlList[position], true, null, null, "title") //隐藏标题和返回键 titleTextView.visibility = View.GONE backButton.visibility = View.GONE //全屏按键 fullscreenButton.setOnClickListener { holder.itemPlayer.startWindowFullscreen(context, false, true) } //防止错位设置 playPosition = position //是否根据视频尺寸,自动选择横竖屏全屏 isAutoFullWithSize = true //音频焦点冲突时是否释放 isReleaseWhenLossAudio = false //全屏动画 isShowFullAnimation = true //不能触摸滑动 setIsTouchWiget(false) } } override fun getItemCount() = urlList.size }
Activity
val videoRecyclerView = findViewById<RecyclerView>(R.id.video_recyclerView) val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) videoRecyclerView.layoutManager = layoutManager //GlobalUrl.urlList为视频链接集合 val videoAdapter = VideoAdapter(GlobalUrl.urlList, this) videoRecyclerView.adapter = videoAdapter videoRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) //可见区域的第一个item val firstPosition = layoutManager.findFirstVisibleItemPosition() //可见区域的最后一个item val lastPosition = layoutManager.findLastVisibleItemPosition() //播放的位置 val playPosition = GSYVideoManager.instance().playPosition if (playPosition >= 0) { //说明有播放 if (playPosition < firstPosition || playPosition > lastPosition) { if (GSYVideoManager.isFullState(this@VideoListActivity)) { return } GSYVideoManager.releaseAllVideos() videoAdapter.notifyDataSetChanged() } } } })
别忘了对返回键和生命周期的处理哦
override fun onBackPressed() { super.onBackPressed() if (GSYVideoManager.backFromWindowFull(this)) { return } super.onBackPressed() } override fun onPause() { super.onPause() GSYVideoManager.onPause() } override fun onResume() { super.onResume() GSYVideoManager.onResume() } override fun onDestroy() { super.onDestroy() GSYVideoManager.releaseAllVideos() }
加载全部内容