未解决的参考:ExoPlayerFactory.newSimpleInstance 中的 ExoPlayerFactory 和类型不匹配:推断类型为 Uri!但 MediaItem 预计在

问题描述 投票:0回答:2

更新到 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. 未解决的参考:ExoPlayerFactory
  2. 类型不匹配:推断类型是 Uri!但 MediaItem 是预期的 在
     .createMediaSource(Uri.parse("asset:///$soundFile"))
  1. 预期变量
exoPlayer.repeatMode = Player.REPEAT_MODE_ALL

屏幕截图ScreenShot

梯度:


    // 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"

尝试过 经过多次搜索 将以下功能更改为 ->

  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”从未使用过

任何帮助将不胜感激。

android kotlin exoplayer exoplayer2.x exoplayer-media-item
2个回答
1
投票

我尝试过这个,现在工作正常。

   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
    }

0
投票

对于 ExoPlayerFactory,替换为 ExoPlayer

//导入com.google.android.exoplayer2.ExoPlayerFactory;替换为

import com.google.android.exoplayer2.ExoPlayer;

public class xxxxx {
    private static yyyy ;

    //add below
    private ExoPlayer exoPlayer;

and somewhere inside your activity
// SimpleExoPlayer newSimpleInstance = SimpleExoPlayer.newSimpleInstance(this.context, defaultTrackSelector) replace with

                    ExoPlayer newSimpleInstance = new ExoPlayer.Builder(this.context)
                            .setTrackSelector(defaultTrackSelector)
                            .build();
© www.soinside.com 2019 - 2024. All rights reserved.