今天,和大家分享一个开源的多功能视频播放器 — 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'
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) {
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
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)
val firstPosition = layoutManager.findFirstVisibleItemPosition()
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()
}
|