gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:25:44 28 4
gpt4 key购买 nike

如何在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com