Exoplayer查看无法找到player.prepare()函数

时间:2018-05-15 05:06:39

标签: android kotlin android-mediaplayer exoplayer exoplayer2.x

目前,我遇到了新版 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
    }

1 个答案:

答案 0 :(得分:0)

prepare()方法是ExoPlayer接口的一部分,该接口扩展了Player接口。

PlayerView仅公开没有准备方法的Player接口。这就是为什么你不能mExoPlayerView!!.player.prepare(mVideoSource)

但是,您使用的ExoPlayerFactory确实会返回一个实现了SimpleExoPlayer接口的ExoPlayer实例:

SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(context), trackSelector, loadControl);
player.prepare(mediaSource);

以下是ExoPlayerPlayer接口以及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