private lateinit var surfaceView: SurfaceView
private lateinit var listener: VideoPlayerListener
private var mContext: Context? = null
constructor(context: Context) : super(context) {
initVideoView(context)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initVideoView(context)
}
constructor(
context: Context,
attrs: AttributeSet?,
@AttrRes defStyleAttr: Int
) : super(context, attrs, defStyleAttr) {
initVideoView(context)
}
private fun initVideoView(context: Context) {
mContext = context
//获取焦点,不知道有没有必要。
isFocusable = true
}
/**
/**
/**
override fun surfaceDestroyed(holder: SurfaceHolder) {}
}
/**
/**
//开启硬解码 ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, “mediacodec”, 1);
mMediaPlayer = ijkMediaPlayer
(mMediaPlayer as IjkMediaPlayer).setOnPreparedListener(listener)
(mMediaPlayer as IjkMediaPlayer).setOnInfoListener(listener)
(mMediaPlayer as IjkMediaPlayer).setOnSeekCompleteListener(listener)
(mMediaPlayer as IjkMediaPlayer).setOnBufferingUpdateListener(listener)
(mMediaPlayer as IjkMediaPlayer).setOnErrorListener(listener)
}
fun setListener(listener: VideoPlayerListener) {
this.listener = listener
if (mMediaPlayer != null) {
mMediaPlayer!!.setOnPreparedListener(listener)
}
}
/**
fun release() {
if (mMediaPlayer != null) {
mMediaPlayer!!.reset()
mMediaPlayer!!.release()
mMediaPlayer = null
}
}
fun pause() {
if (mMediaPlayer != null) {
mMediaPlayer!!.pause()
}
}
fun stop() {
if (mMediaPlayer != null) {
mMediaPlayer!!.stop()
}
}
fun reset() {
if (mMediaPlayer != null) {
mMediaPlayer!!.reset()
}
}
fun getDuration(): Long {
return if (mMediaPlayer != null) {
mMediaPlayer!!.duration
} else {
0
}
}
fun getCurrentPosition(): Long {
return if (mMediaPlayer != null) {
mMediaPlayer!!.currentPosition
} else {
0
}
}
fun seekTo(l: Long) {
if (mMediaPlayer != null) {
mMediaPlayer!!.seekTo(l)
}
}
}
abstract class VideoPlayerListener : IMediaPlayer.OnBufferingUpdateListener,
IMediaPlayer.OnCompletionListener, IMediaPlayer.OnPreparedListener, IMediaPlayer.OnInfoListener,
IMediaPlayer.OnVideoSizeChangedListener, IMediaPlayer.OnErrorListener,
IMediaPlayer.OnSeekCompleteListener
当实现这个抽象类的时候,最重要的是在onPrepared()方法中,让视频开始播放。
android:layout_width="@dimen/dimen_0"
android:layout_height="@dimen/dimen_0"
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf="@id/add_video"
android:layout_marginTop="@dimen/dimen_16"
android:layout_marginBottom="@dimen/dimen_100"/>
初始化:
//加载native库
try {
IjkMediaPlayer.loadLibrariesOnce(null)
IjkMediaPlayer.native_profileBegin(“libijkplayer.so”)
} catch (e: Exception) {
this.finish()
}
//使用
binding.ijkPlayer.setVideoPath(filePath)
binding.ijkPlayer.setListener(object : VideoPlayerListener() {
override fun onBufferingUpdate(p0: IMediaPlayer?, p1: Int) {
TODO(“Not yet implemented”)
}
override fun onCompletion(p0: IMediaPlayer?) {
TODO(“Not yet implemented”)
}
//准备好之后播放视频
override fun onPrepared(p0: IMediaPlayer?) {
p0!!.start()
}
override fun onInfo(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
最后针对Android程序员,我这边给大家整理了一些资料,包括不限于高级UI、性能优化、移动架构师、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
[外链图片转存中…(img-7rDPLrGv-1643533529655)]
最后针对Android程序员,我这边给大家整理了一些资料,包括不限于高级UI、性能优化、移动架构师、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录