如何在 PlaybackTransportControlGlue android TV 上动态更改字幕
Posted
技术标签:
【中文标题】如何在 PlaybackTransportControlGlue android TV 上动态更改字幕【英文标题】:How to change subtile dynamically on PlaybackTransportControlGlue android TV 【发布时间】:2021-08-26 06:41:19 【问题描述】:我正在为 android TV 构建一个视频播放器应用。我正在使用 https://developer.android.com/training/tv/playback/transport-controls 中解释的 Exoplayer 依赖依赖项。 到目前为止,我已经能够显示静态的视频标题,但我需要显示一个动态的字幕,它会随着视频播放的变化而变化。我该怎么做? 下图显示了视频播放器的外观。我使用了一个字幕短语作为它应该出现的位置的占位符。
【问题讨论】:
【参考方案1】:我能够解决问题。我在 VideoSupportFragment 类 Player.EventListener 中添加了一个侦听器。 这样:
class VideoFragment(mediaItems: Map<String, Any>) : VideoSupportFragment(), Player.EventListener
private var playerAdapter: ExoPlayerAdapter? = null
private var _mediaItems: Map<String, Any>? = null
private lateinit var mMediaPlayerGlue: VideoMediaPlayerGlue<ExoPlayerAdapter>
private var mItems: List<*>? = null
init
_mediaItems = mediaItems
private val mHost: VideoSupportFragmentGlueHost = VideoSupportFragmentGlueHost(this)
private fun playWhenReady(glue: PlaybackGlue)
if (glue.isPrepared)
glue.play()
else
glue.addPlayerCallback(object : PlaybackGlue.PlayerCallback()
override fun onPreparedStateChanged(glue: PlaybackGlue)
if (glue.isPrepared)
glue.removePlayerCallback(this);
glue.play()
)
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
val mediaSource = _mediaItems?.get("media_source") as Map<*, *>
playerAdapter = ExoPlayerAdapter(activity!!, _mediaItems!!)
mMediaPlayerGlue =
VideoMediaPlayerGlue(activity, playerAdapter!!)
mMediaPlayerGlue.host = mHost;
mMediaPlayerGlue.isControlsOverlayAutoHideEnabled = true
mItems = mediaSource["media_items"] as List<*>
mMediaPlayerGlue.title = mediaSource["title"] as CharSequence?
mMediaPlayerGlue.playerAdapter.setDataSource()
mMediaPlayerGlue.isSeekEnabled = true
playerAdapter?.player?.addListener(this);
playWhenReady(mMediaPlayerGlue)
override fun onPause()
super.onPause()
playerAdapter?.player?.pause()
override fun onResume()
super.onResume()
playerAdapter?.player?.play()
override fun onDestroy()
super.onDestroy()
playerAdapter?.player?.removeListener(this);
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int)
if (mItems?.size!! > 1)
val item : Map<*, *> = mItems!![playerAdapter?.player?.currentWindowIndex!!] as Map<*, *>
mMediaPlayerGlue.subtitle = item["subtitle"] as String
【讨论】:
以上是关于如何在 PlaybackTransportControlGlue android TV 上动态更改字幕的主要内容,如果未能解决你的问题,请参考以下文章
如何在异步任务中调用意图?或者如何在 onPostExecute 中开始新的活动?