How to skip MP4 video in playlist when error in exoplayer

Viewed 935

I am using ExoPlayer for playing video in Android. I have 3 local mp4 files and want to create playlist from that files. I'm use ConcatenatingMediaSource to add all files to one playlist.

    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

    }

Unfortunately, some files produce error when playing and make exoplayer crash (not play other video).

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)

My question is, how to catch that error so I can skip the file and continue playing other files?

Thank you

1 Answers

You need to add event listener to exo player and handle errors there

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)
        }
    }
Related