更新到 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
}
错误是
.createMediaSource(Uri.parse("asset:///$soundFile"))
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"
尝试过 经过多次搜索 将以下功能更改为 ->
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”从未使用过
任何帮助将不胜感激。
我尝试过这个,现在工作正常。
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
}
对于 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();