如何在 ExoPlayer2 中显示 VTT 字幕?
How to show VTT subtitles in ExoPlayer2?
如何在 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)
}
我没有得到任何异常,它只是没有显示任何东西……
没有什么真正有效...真的需要一些帮助,在此先感谢您!
这是我使用的代码(这些是我从网上找的,忘记了)
// 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 设置和显示来自 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)
}
我没有得到任何异常,它只是没有显示任何东西…… 没有什么真正有效...真的需要一些帮助,在此先感谢您!
这是我使用的代码(这些是我从网上找的,忘记了)
// 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)