android ijkplayer简单使用
作者:互联网
class VideoPlayer : FrameLayout, TextureView.SurfaceTextureListener{ private var url:String? = null private var headers:Map<String,String>? = null private var mediaPlayer: IMediaPlayer? = null //播放器 private var container: FrameLayout? = null //播放器根目录 private var textureView: TextureView? = null //视频显示视图 private var surfaceTexture: SurfaceTexture? = null private var playerType:Int = PlayerTypes.TYPE_IJK private var surface: Surface? = null constructor(context: Context, attributeSet: AttributeSet):super(context,attributeSet){ init() } /** * 设置播放器类型 * * @param playerType IjkPlayer or MediaPlayer. */ fun setPlayerType(playerType: Int) { this.playerType = playerType } fun setUp(url:String,headers:Map<String,String>?){ this.url = url this.headers = headers } fun start(){ mediaPlayer?.start() } private fun init(){ container = FrameLayout(context) this.removeView(container) val params = LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ) this.addView(container,params) initTextureView() initMediaPlayer() } private fun initTextureView(){ if(textureView == null){ textureView = TextureView(context) textureView?.surfaceTextureListener = this } container?.removeView(textureView) val params = LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER ) container?.addView(textureView,0,params) } private fun initMediaPlayer(){ if(mediaPlayer == null){ when(playerType){ PlayerTypes.TYPE_IJK -> mediaPlayer = IjkMediaPlayer() PlayerTypes.TYPE_NATIVE -> mediaPlayer = AndroidMediaPlayer() else -> mediaPlayer = IjkMediaPlayer() } } mediaPlayer?.setOnVideoSizeChangedListener { player, w, h, sar_num, sar_den -> Log.d("======","setOnVideoSizeChangedListener") } mediaPlayer?.setOnErrorListener { player, what, extra -> Log.d("======","setOnErrorListener") true } mediaPlayer?.setOnCompletionListener { Log.d("======","setOnCompletionListener") } mediaPlayer?.setOnPreparedListener { Log.d("======","setOnPreparedListener") } mediaPlayer?.setOnInfoListener { player, what, extra -> Log.d("======","setOnInfoListener") true } mediaPlayer?.setOnBufferingUpdateListener { player, percent -> Log.d("======","setOnBufferingUpdateListener") } openMediaPlayer() } private fun openMediaPlayer(){ mediaPlayer?.setDataSource(context, Uri.parse(url),headers) mediaPlayer?.prepareAsync() } override fun onSurfaceTextureSizeChanged(p0: SurfaceTexture?, p1: Int, p2: Int) { } override fun onSurfaceTextureUpdated(p0: SurfaceTexture?) { } override fun onSurfaceTextureDestroyed(p0: SurfaceTexture?): Boolean { return p0 == null } override fun onSurfaceTextureAvailable(p0: SurfaceTexture?, p1: Int, p2: Int) { if(surfaceTexture == null){ surfaceTexture = p0 if(surface == null){ surface = Surface(surfaceTexture) } mediaPlayer?.setSurface(surface) }else{ textureView?.surfaceTexture = surfaceTexture } } }
就是简单实现播放功能,其他的以后在添加
video_view.setPlayerType(PlayerTypes.TYPE_IJK)
video_view.setUp(VideoDatas.videoUrls[2],null)
video_view.start()
标签:ijkplayer,textureView,mediaPlayer,private,var,简单,fun,android,null 来源: https://www.cnblogs.com/rchao/p/11190863.html