如何在 android 中实现 Exoplayer 2.11.1?

Posted

技术标签:

【中文标题】如何在 android 中实现 Exoplayer 2.11.1?【英文标题】:How to implement Exoplayer 2.11.1 in android? 【发布时间】:2020-04-13 19:34:55 【问题描述】:

我正在尝试实现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 构建音乐流媒体应用程序。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

通过新的更新,您可以使用SimpleExoPlayer.Builder 创建一个简单的播放器实例:

simpleExoplayer = SimpleExoPlayer.Builder(context).build()

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

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

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
        
    

【讨论】:

样本是用哪种语言编写的?是 dart 还是 kotlin? @Behrouz.M 是 kotlin【参考方案2】:

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

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)

【讨论】:

谢谢,它对我有用,感谢您提供额外的信息和链接。【参考方案3】:

Java 专家(Java 万岁)

活动中

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

公共职能

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'

【讨论】:

【参考方案4】:

直到 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

【讨论】:

以上是关于如何在 android 中实现 Exoplayer 2.11.1?的主要内容,如果未能解决你的问题,请参考以下文章

Recyclerview 中的 Exoplayer 错误,源错误没有可用的提取器

如何在android中实现svm模型?

如何在android中实现BottomAppBar?

使用 ExoPlayer 的 Android MediaPlayer 持久缓存

ExoPlayer onResume() 恢复播放状态

如何在 android 中更改 ExoPlayer 字幕位置?