kotlin 未解析的引用:ExoPlayerFactory中的ExoPlayerFactory. newSimpleInstance和类型不匹配:推断的类型为Uri!,但应为MediaItem

gpfsuwkq  于 2022-12-27  发布在  Kotlin
关注(0)|答案(1)|浏览(211)

在更新到ExoPlayer的最新构建版本(即"2.18.1")之后,ExoPlayerFactory. newSimpleInstance显示未解析的引用错误,
想重新格式化这个初始化函数到exoplayer的最新版本而不改变它的逻辑
获取函数中的模糊错误

private fun initializeExoPlayer(soundFile: String): ExoPlayer {
        // create the player
        val exoPlayer = ExoPlayerFactory.newSimpleInstance(
            DefaultRenderersFactory(this), DefaultTrackSelector()
        )

        // load the media source
        val dataSource = DefaultDataSourceFactory(this,
            Util.getUserAgent(this, this.getString(R.string.app_name)))
        val mediaSource = ProgressiveMediaSource.Factory(dataSource)
            .createMediaSource(Uri.parse("asset:///$soundFile"))

        // load the media
        Log.d("MAIN", "loading $soundFile")
        exoPlayer.prepare(mediaSource)
        // loop indefinitely
        exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

        return exoPlayer
    }

错误包括
1.未解析的引用:ExoPlayer工厂
1.类型不匹配:推断的类型为Uri!,但应为MediaItem

.createMediaSource(Uri.parse("asset:///$soundFile"))

1.变量应为

exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

屏幕截图

分级:

// ExoPlayer
    api "com.google.android.exoplayer:exoplayer-core:2.18.1"
    api "com.google.android.exoplayer:exoplayer-ui:2.18.1"
    api "com.google.android.exoplayer:extension-mediasession:2.18.1"
    • TRIED**多次搜索后将以下函数更改为-〉
private fun initializeExoPlayer(soundFile: String): ExoPlayer {

        // create the player
        val exoPlayer = ExoPlayer.Builder(this).build()

        // load the media source
        val dataSource = DefaultDataSourceFactory(this,
                Util.getUserAgent(this, this.getString(R.string.app_name)))

        val firstAudioUri = Uri.parse("assets:///$soundFile")
        val mediaSource = MediaItem.fromUri(firstAudioUri)

        // load the media
        Log.d("MAIN", "loading $soundFile")
        exoPlayer.addMediaItem(mediaSource)
        exoPlayer.prepare()
        // loop indefinitely
        exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

        return exoPlayer
    }

所有指示的错误均已消失,但未播放媒体,且从未使用变量"dataSource"
任何帮助都将不胜感激。

w8f9ii69

w8f9ii691#

我试过这个了,现在效果很好。

private fun initializeExoPlayer(soundFile: String): ExoPlayer {

        // create the player
        val trackSelector = DefaultTrackSelector(this)
        val exoPlayer = ExoPlayer.Builder(this).setTrackSelector(trackSelector).build()

        // load the media source
        val dataSource = DefaultDataSource.Factory(this)

        val mediaSource = ProgressiveMediaSource.Factory(dataSource)
            .createMediaSource(MediaItem.fromUri(Uri.parse("asset:///$soundFile")))

        // load the media
        Log.d("MAIN", "loading $soundFile")
        exoPlayer.setMediaSource(mediaSource)
        exoPlayer.prepare()
      
        // loop indefinitely
        exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

        return exoPlayer
    }

相关问题