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
}
/**
- 设置视频地址。
- 根据是否第一次播放视频,做不同的操作。
- @param path the path of the video.
*/
fun setVideoPath(path: String) {
if (TextUtils.equals("", mPath)) {
//如果是第一次播放视频,那就创建一个新的surfaceView
mPath = path
createSurfaceView()
} else {
//否则就直接load
mPath = path
load()
}
}
/**
- 新建一个surfaceview
*/
private fun createSurfaceView() {
//生成一个新的surface view
surfaceView = SurfaceView(mContext)
surfaceView.holder.addCallback(LmnSurfaceCallback())
val layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER
)
surface