如何在 android 中实现 Exoplayer 2.11.1?

How to implement Exoplayer 2.11.1 in android?

我正在尝试实施 exoplayer 这是我的 exoplayer 版本

implementation 'com.google.android.exoplayer:exoplayer:2.11.1'

我正在创建一个音乐播放器应用程序,但我对 exoplayer 一无所知 我正在尝试实施过去 2 天的 exoplayer,但它不起作用。我无法理解官方文档中的任何内容。

我找到了很多示例和教程,但都是关于使用 exoplayer 播放视频的。许多示例都在使用已弃用的方法。

我正在尝试使用此 tutorial 来实现,但许多方法已被弃用,因此无法正常工作 例如

simpleExoplayer = ExoPlayerFactory.newSimpleInstance(
            DefaultRenderersFactory(this),
            DefaultTrackSelector(adaptiveTrackSelectionFactory),
            DefaultLoadControl()
        )

任何人都可以建议我从哪里开始或如何使用最新版本的 exoplayer 构建音乐流媒体应用程序。

任何帮助将不胜感激。

有了新的更新,您可以使用 SimpleExoPlayer.Builder:

创建一个简单的播放器实例
simpleExoplayer = SimpleExoPlayer.Builder(context).build()

您还可以为 Builder 提供不同的参数。参见 https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html

您可以使用我创建的这个简单的自定义 class 来帮助您入门。

class ExoPlayerHelper(
    private val playerView: PlayerView,
    onError: (ExoPlaybackException) -> Unit,
    onPlayerBuffer: (Boolean) -> Unit
) {

    private var exoPlayer: ExoPlayer? = null
    private var mediaSource: ProgressiveMediaSource? = null

    private val playerListener = object : Player.EventListener {
        override fun onPlayerError(error: ExoPlaybackException) {
            super.onPlayerError(error)
            onError(error)
        }

        override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
            super.onPlayerStateChanged(playWhenReady, playbackState)
            onPlayerBuffer(playbackState == Player.STATE_BUFFERING)
        }
    }

    fun initializePlayer(url: String) {
        exoPlayer = SimpleExoPlayer.Builder(playerView.context).build()
        exoPlayer!!.repeatMode = Player.REPEAT_MODE_ALL
        exoPlayer!!.addListener(playerListener)

        playerView.player = exoPlayer

        val userAgent =
            Util.getUserAgent(playerView.context, playerView.context.getString(R.string.app_name))
        mediaSource = ProgressiveMediaSource
            .Factory(
                DefaultDataSourceFactory(playerView.context, userAgent),
                DefaultExtractorsFactory()
            )
            .createMediaSource(Uri.parse(url))

        exoPlayer!!.prepare(mediaSource!!, true, false)
        exoPlayer!!.playWhenReady = true
    }

    private fun killPlayer() {
        if (exoPlayer != null) {
            exoPlayer!!.release()
            exoPlayer = null
            mediaSource = null
            playerView.player = null
        }
    }
}

我也遇到过这个问题,这是我的解决方案

Declaration

private val exoPlayer: SimpleExoPlayer by lazy { SimpleExoPlayer.Builder(this).build()}

播放歌曲

private fun prepareExoPlayerFromURL(url: String) {
        val dataSourceFactory =
            DefaultDataSourceFactory(this, Util.getUserAgent(this, resources.getString(R.string.app_name)), null)
        val extractorsFactory = DefaultExtractorsFactory()
//        val audioSource = ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, null, null)

        val concateMediaSource = ConcatenatingMediaSource()

        // to play from song list
        for (i in mSongList) {      // song list song arraylist
            val mediaSource = ProgressiveMediaSource
                .Factory(
                    DefaultDataSourceFactory(this, dataSourceFactory),
                    DefaultExtractorsFactory()
                )
                .createMediaSource(Uri.parse(i.musicFile)/*Uri.parse(i.uri)*/)
            concateMediaSource.addMediaSource(mediaSource)
        }

        // to play single song
       /* val audioSource = ProgressiveMediaSource
            .Factory(
                DefaultDataSourceFactory(this, dataSourceFactory),
                DefaultExtractorsFactory()
            )
            .createMediaSource(Uri.parse(url))*/

        exoPlayer.prepare(concateMediaSource/*audioSource*/)
        exoPlayer.seekToDefaultPosition(songPosition)
        exoPlayer.playWhenReady = true

        setNotification()
    }

设置播放器的监听器和通知

private fun setListoner() {
        exoPlayer.addListener(object : Player.EventListener {
            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                when (playbackState) {
                    Player.STATE_BUFFERING -> Log.e(TAG,"STATE_BUFFERING")
                    Player.STATE_ENDED -> Log.e(TAG,"STATE_ENDED")
                    Player.STATE_IDLE -> Log.e(TAG,"STATE_IDLE")
                    Player.STATE_READY ->{
                        if (playWhenReady) {
                            Log.e(TAG, "PlaybackStatus.PLAYING")
                        } else {
                            Log.e(TAG, "PlaybackStatus.PAUSED")
                        }
                    }
                    else -> Log.e(TAG,"PlaybackStatus.IDLE")
                }
            }
        })
    }

杀死玩家

 private fun onDestroy() {
        if (exoPlayer != null) {
            exoPlayer.release()
            exoPlayer = null
            mediaSource = null
        }
    }

更多细节你可以看official documentation

Universal Music Player使用ExoPlayer进行本地音频播放。

Building feature-rich media apps with ExoPlayer (Google I/O '18)

致 Java 伙计们(万岁 Java)

在Activity

private PlayerView epPlayerView = findViewById(R.id.design_reference);

Public函数

public static void runExoPlayer(PlayerView epPlayerView,
                                String url,
                                Context context) {

    Uri videoUri = Uri.parse(url);

    SimpleExoPlayer  exoPlayer = new SimpleExoPlayer.Builder(context).build();
    epPlayerView.setPlayer(exoPlayer);

    MediaItem mediaItem = MediaItem.fromUri(videoUri);
    exoPlayer.clearMediaItems();
    exoPlayer.setMediaItem(mediaItem);
    exoPlayer.prepare();
    exoPlayer.setPlayWhenReady(true);

}

建造Gradle

   // Exo Media Player
    implementation 'com.google.android.exoplayer:exoplayer:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-core:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-hls:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.15.1'
    androidTestImplementation 'androidx.test:rules:1.4.0'

直到 2.15.0 版本,您可以创建 SimpleExoPlayer 实例如下:

SimpleExoPlayer.Builder(this)
            .setMediaSourceFactory(mediaSourceFactory)
            .build() 

在 2.16.0 版本中,SimpleExoPlayer 已被弃用,您应该改用 ExoPlayer 实例。您可以按以下方式创建它:

ExoPlayer.Builder(this)
            .setMediaSourceFactory(mediaSourceFactory)
            .build()

https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html