VideoView 播放FFmpeg 处理成libx264的mp4文件视频黑屏,但是声音正常

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VideoView 播放FFmpeg 处理成libx264的mp4文件视频黑屏,但是声音正常相关的知识,希望对你有一定的参考价值。

将VideoView 切换到ExoPlayer 即可。

与VideoView不同。

版本 2.18.1 下面是替换后的代码。

class CustomVideo : PlayerView 
    lateinit var player: SimpleExoPlayer
    private var mLocalMuteFlag = false // 本地静音标识
    private var mAction: PlayerUtil.OnMediaListener? = null
    private var mProgressTask: CoroutinesTask? = null // 进度任务
    private var mMediaDuration = 0L // 多媒体时长

    init 
        this.apply 
            controllerAutoShow = false
            controllerHideOnTouch = false
            controllerShowTimeoutMs = 0
//        controllerVisibilityListener = object : PlayerControlView.VisibilityListener 
//                override fun onVisibilityChange(visibility: Int) 
//                    // Do nothing
//                
//            
        
    

    // 设置播放事件
    fun setOnActionListener(listener: PlayerUtil.OnMediaListener?) 
        mAction = listener
    

    // 设置多媒体路径
    fun setMediaUri(path: String) = catchException(
        if (player.isPlaying) player.stop()
        releaseProgressTask()
        val dataSourceFactory = DefaultDataSourceFactory(context)
        val videoUri = Uri.parse(path)
        val mediaItem: MediaItem = MediaItem.fromUri(videoUri)

        val mediaSource =
            ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem)
        player.prepare(mediaSource)
    )

    // 播放视频
    fun playVideo() = catchException(
        if (!player.isPlaying) 
            player.playWhenReady = true
            startProgressTask()
            mAction?.onMediaStart()
        
    )

    // 暂停播放
    fun pauseVideo() = catchException(
        if (player.isPlaying) 
            player.playWhenReady = false
            mAction?.onMediaPause()
        
    )

    // 静音
    fun updateMuteStatus(muteFlag: Boolean) 
        mLocalMuteFlag = muteFlag
        val volume = if (muteFlag) 0f else 1f
        player.volume = volume
    

    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) 
        player = SimpleExoPlayer.Builder(context).build()


        player.addListener(object : Player.Listener 
            override fun onPlayerError(error: PlaybackException) 
                mAction?.onMediaErr("media err what:$error.errorCode extra:$error.cause")
            

            override fun onPlaybackStateChanged(state: Int) 
                if (state == Player.STATE_ENDED) 
                    mAction?.onMediaComplete()
                
            
        )

        player.addAnalyticsListener(object : AnalyticsListener 
            override fun onPositionDiscontinuity(
                eventTime: AnalyticsListener.EventTime, reason: Int
            ) 
                if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) 
                    val position = player.currentPosition
                    val duration = player.duration
                    val progress = (position * 100f / duration).toInt()
                    mAction?.onMediaPlaying(
                        progress = progress, position = position, duration = duration
                    )
                
            
        )


        setPlayer(player)
    

    // 开始进度任务
    private fun startProgressTask() 
        if (mProgressTask == null) mProgressTask = CoroutinesTask()
        if (!player.isPlaying) return
        if (mMediaDuration == 0L) mMediaDuration = (player.duration).toLong()
        val position = (player.currentPosition).toLong()
        val lastProgress = (position * 100f) / mMediaDuration
        val progress = if (mMediaDuration <= 0) 0 else (lastProgress + 0.6f).toInt()
        mAction?.onMediaPlaying(progress = progress, position = position, duration = mMediaDuration)
        mProgressTask?.launchDelayRequest(200)  startProgressTask() 
    

    // 释放进度任务
    private fun releaseProgressTask() 
        mProgressTask?.destroyTask()
        mProgressTask = null
    

    override fun onDetachedFromWindow() 
        super.onDetachedFromWindow()
        releaseProgressTask()
        player.release()

    

以上是关于VideoView 播放FFmpeg 处理成libx264的mp4文件视频黑屏,但是声音正常的主要内容,如果未能解决你的问题,请参考以下文章

ffmpeg处理视频与声音

使用ffmpeg转码的MP4文件需要加载完了才能播放的解决办法

带有“ 307临时重定向”的Android VideoView播放URL

Android视频播放软解与硬解的区别

转 Android视频播放软解与硬解的区别

Android上的实时视频监控怎么实现的?