目前我遇到了新版 Exoplayer 的问题。下面是用于播放器启动的代码。在 getPlayerStart() 我正在传递 url 链接。在 initExoPlayer() 中,我正在初始化我的 Exoplayer 但在 Exoplayer 的早期版本中我遇到了这个问题:
mExoPlayerView!!.player.prepare(mVideoSource):
以上功能显示不可用
private fun getPlayerStart(urlLink: String) {
if (playerMode) {
val userAgent = Util.getUserAgent(context, context!!.getApplicationInfo().packageName)
val httpDataSourceFactory = DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true)
val dataSourceFactory = DefaultDataSourceFactory(context, null, httpDataSourceFactory)
Log.i("Video",urlLink)
val daUri = Uri.parse(urlLink)
val extractorsFactory = DefaultExtractorsFactory()
mVideoSource = ExtractorMediaSource(daUri, dataSourceFactory,
extractorsFactory, null, null) as MediaSource?
}
initExoPlayer()
}
private fun initExoPlayer() {
val bandwidthMeter = DefaultBandwidthMeter()
val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
val loadControl = DefaultLoadControl()
player = ExoPlayerFactory.newSimpleInstance(DefaultRenderersFactory(context), trackSelector, loadControl)
mExoPlayerView!!.player= this.player
player!!.addListener(this)
mExoPlayerView!!.player.prepare(mVideoSource)
mExoPlayerView!!.getPlayer().playWhenReady = true
}
最佳答案
prepare()
方法是 ExoPlayer
接口(interface)的一部分,它扩展了 Player
接口(interface)。
PlayerView
只暴露了Player
接口(interface),没有prepare 方法。这就是为什么你不能执行 mExoPlayerView!!.player.prepare(mVideoSource)
的原因。
但是,您正在使用的 ExoPlayerFactory
会返回一个 SimpleExoPlayer
实例,该实例确实实现了 ExoPlayer
接口(interface):
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(context), trackSelector, loadControl);
player.prepare(mediaSource);
下面是ExoPlayer
和Player
接口(interface)的类引用,还有SimpleExoPlayer
:
https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/Player.html https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ExoPlayer.html https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.html
关于android - Exoplayer View 无法找到 player.prepare() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50342675/