今天,和大家分享一个开源的多功能视频播放器 — GSYVideoPlayer,支持弹幕,滤镜、水印、gif截图,片头广告,声音、亮度调节等众多功能,这里就利用它来实现一个标准的视频播放器,那么,话不多说,Go ~

引入依赖

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

android:name=".MainActivity"

android:exported="true"

android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"

android:screenOrientation="fullSensor" >

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">

android:id="@+id/videoPlayer"

android:layout_width="match_parent"

android:layout_height="match_parent" />

设置要播放的视频链接

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".VideoListActivity">

android:id="@+id/video_recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

编写item布局

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingBottom="30dp">

android:id="@+id/item_player"

android:layout_width="match_parent"

android:layout_height="300dp" />

RecyclerView适配器

class VideoAdapter(private val urlList: List, private val context: Context) :

RecyclerView.Adapter() {

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(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()

}

查看原文