对于 ExoPlayer 2.18.2,我正在初始化一个 DefaultTrackSelector
trackSelector = DefaultTrackSelector(requireContext())
并将其附加到播放器
private fun createContentPlayer() {
val renderersFactory = DefaultRenderersFactory(requireContext())
this.trackSelector = DefaultTrackSelector(requireContext())
val player = ExoPlayer.Builder(requireContext())
.setRenderersFactory(renderersFactory)
.setTrackSelector(this.trackSelector!!)
.build()
this.contentPlayer = player
}
所以我有这个方法来检索文本渲染器索引
private fun getTextRenderIndex(): Int {
val count = contentPlayer?.rendererCount ?: 0
for (i: Int in 0..count) {
if (contentPlayer?.getRendererType(i) == C.TRACK_TYPE_TEXT) {
return i
}
}
return -1
}
文本渲染器索引始终为 2(请参见下面屏幕截图中的索引)
媒体源是这样准备的
val uri = Uri.parse("video.m3u8")
val dataSourceFactory = DefaultDataSource.Factory(requireContext())
val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(uri))
contentPlayer?.setMediaSource(mediaSource)
contentPlayer?.prepare()
好的,到目前为止没问题,但是如果我打电话
val mappedTrackInfo = trackSelector?.currentMappedTrackInfo
然后在调试器中查看 MappedTrackInfo 对象,渲染器名称是
["MediaCodecVideo...", "MediaCodecAudio...", "TextRenderer", "MetadataRendere...", "CameraMotionRen..."]
但是同一对象中的 rendererTrackGroups 显示没有文本轨道
为什么缺少文本渲染器?
应用程序正在播放的视频是 .m3u8,并且通常嵌入了隐藏式字幕。但到目前为止,我还没有看到任何隐藏式字幕渲染。所以我猜原因是文本渲染轨道不存在。
我的目标是能够使用以下代码打开/关闭视频的隐藏式字幕
val length = trackGroupArray?.length ?: 0
if (length > 0) {
val trackGroup = trackGroupArray!![0]
val currentParamsBuilder = trackSelector?.buildUponParameters()
?: DefaultTrackSelector.Parameters.Builder(requireContext())
val disabled = !enabled
if (trackGroup.length > 0) {
val selectionOverride = TrackSelectionOverride(trackGroup, 0)
val newParameters = currentParamsBuilder
.setRendererDisabled(textRenderIndex, disabled)
.clearOverridesOfType(TRACK_TYPE_TEXT)
.addOverride(selectionOverride)
.build()
trackSelector?.parameters = newParameters
}
}