android - 如何在 ExoPlayer2 中显示 VTT 字幕?

标签 android kotlin exoplayer2.x subtitle

如何在 Android 中使用 ExoPlayer2 设置和显示来自 url 的字幕?目前,我用 Kotlin 编写,我使用以下代码设置带字幕的 ExoPlayer:

exoPlayer = SimpleExoPlayer.Builder(this).build()
    val subtitle = MediaItem.Subtitle(Uri.parse(SUBTITLES_URL), MimeTypes.TEXT_VTT, "en")
    val subtitles = arrayListOf(subtitle)
    val mediaItem = MediaItem.Builder()
        .setUri(movieSrc)
        .setSubtitles(subtitles)
        .build()
    exoPlayer.setMediaItem(mediaItem)
    exoPlayer.addListener(this)
    exoPlayer.prepare()
以及在 ExoPlayer SubtitleView 中显示它们的以下代码:
exoPlayer.addTextOutput {
        binding.exoSubtitles.onCues(it)
    }
我没有任何异常,它只是没有显示任何 idk ......
没有什么真正的工作......真的需要一些帮助,提前谢谢你!

最佳答案

这是我使用的代码(我从互联网上忘记了这些代码)

// create the simple cache
val leastRecentlyUsedCacheEvictor = LeastRecentlyUsedCacheEvictor(MAX_CACHE_SIZE)
val databaseProvider: DatabaseProvider = ExoDatabaseProvider(this)
val simpleCache = SimpleCache(cacheDir, leastRecentlyUsedCacheEvictor, databaseProvider)


// create http datasource
val defaultBandwidthMeter = DefaultBandwidthMeter.Builder(context).build()
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
            context,
            DefaultHttpDataSourceFactory(
                System.getProperty("http.agent"),
                defaultBandwidthMeter
            )
        )

// create the player
val simplePlayer = ExoPlayerFactory.newSimpleInstance(context)
        simplePlayer.addListener(playerCallback)
        cacheDataSourceFactory = CacheDataSourceFactory(
            simpleCache,
            DefaultHttpDataSourceFactory(
                Util.getUserAgent(
                    context,
                    "exo"
                )
            )
        )

// create subtitle text format
val textFormat = Format.createTextSampleFormat(
                    null,
                    MimeTypes.TEXT_VTT,
                    C.SELECTION_FLAG_DEFAULT,
                    null
                )
// create the subtitle source
val subtitleSource = SingleSampleMediaSource.Factory(dataSourceFactory)
  .createMediaSource(Uri.parse(it), textFormat, C.TIME_UNSET)
                
// create the media source based from video/audio url
val mediaSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory).createMediaSource(uri) 

// merge subtitle source and media source   
val mediaSourceWithsubtitle = MergingMediaSource(mediaSource, subtitleSource)

// setup the player
simplePlayer.prepare(mediaSourceWithsubtitle, true, true)

关于android - 如何在 ExoPlayer2 中显示 VTT 字幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65274951/

相关文章:

kotlin - 为什么 Kotlin 中的数据类不能用内部修饰符标记?

android - ExoPlayer用来自特定站点的URL口吃

kotlin - 如何在没有 NoSuchMethodError 的 Kotlin 1.4 的 Gradle 构建中使用新定义的委托(delegate)属性?

Kotlin 构建器与构造器

android - Exoplayer View 无法找到 player.prepare() 函数

android - 是否可以制作水平的 NumberPicker?

android - 为什么 LayoutInflater 会忽略我指定的 layout_width 和 layout_height 布局参数?

php - 将大型 JSON 数据从 php mysql 服务器加载到 java android 客户端

android - 如何在Android屏幕上显示HTTP请求的异常