exoplayer错误时如何跳过播放列表中的MP4视频

时间:2019-07-01 07:41:29

标签: android exoplayer

我正在使用ExoPlayer在Android中播放视频。我有3个本地mp4文件,并希望从该文件创建播放列表。我正在使用ConcatenatingMediaSource将所有文件添加到一个播放列表中。

    override fun playFromList(list: List<VideoEntity>) {

        val userAgent = Util.getUserAgent(this, getString(R.string.app_name))
        //2
        val concatenatedSource = ConcatenatingMediaSource()

        list.forEach {

            if (it.localPath != null) {
                val dataSourceFactory = DefaultDataSourceFactory(this, userAgent)
                val extractorFactory = DefaultExtractorsFactory()

                val mediaSource = ProgressiveMediaSource
                        .Factory(dataSourceFactory, extractorFactory)
                        .createMediaSource(Uri.parse(it.localPath))

                concatenatedSource.addMediaSource(mediaSource)
            }

            exoPlayer.prepare(concatenatedSource)
        }

        exoPlayer.playWhenReady = true
        exoPlayerView.player = exoPlayer

    }

不幸的是,某些文件在播放时会产生错误,并使exoplayer崩溃(不能播放其他视频)。

E/ExoPlayerImplInternal: Source error.
    com.google.android.exoplayer2.source.UnrecognizedInputFormatException: None of the available extractors (MatroskaExtractor, FragmentedMp4Extractor, Mp4Extractor, Mp3Extractor, AdtsExtractor, Ac3Extractor, TsExtractor, FlvExtractor, OggExtractor, PsExtractor, WavExtractor, AmrExtractor, Ac4Extractor) could read the stream.
        at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractorHolder.selectExtractor(ProgressiveMediaPeriod.java:1059)
        at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:947)
        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:381)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

我的问题是,如何捕获该错误,以便我可以跳过文件并继续播放其他文件?

谢谢

1 个答案:

答案 0 :(得分:0)

您需要将事件侦听器添加到exo播放器并在那里处理错误

fun onPlayerError(error: ExoPlaybackException) {
        when (error.type) {
            ExoPlaybackException.TYPE_SOURCE -> Log.e(TAG, "TYPE_SOURCE: " + error.sourceException.message)

            ExoPlaybackException.TYPE_RENDERER -> Log.e(TAG, "TYPE_RENDERER: " + error.rendererException.message)

            ExoPlaybackException.TYPE_UNEXPECTED -> Log.e(TAG, "TYPE_UNEXPECTED: " + error.unexpectedException.message)
        }
    }