[英]How to implement Exoplayer 2.11.1 in android?
i am trying to implement exoplayer this is my exoplayer
version我正在尝试实现exoplayer这是我的
exoplayer
版本
implementation 'com.google.android.exoplayer:exoplayer:2.11.1'
i am creating a music player app and i don't know anything about exoplayer
i am trying to implement exoplayer
from last 2 days but it's not working.我正在创建一个音乐播放器应用程序,但我对
exoplayer
我试图从过去 2 天开始实施exoplayer
,但它不起作用。 i couldn't understand anything in the official documentation .我无法理解官方文档中的任何内容。
i find many example and tutorial but it's all about playing video using exoplayer
.我找到了很多示例和教程,但都是关于使用
exoplayer
播放视频。 many example's are using deprecated methods.许多示例都使用不推荐使用的方法。
I am trying to implement using this tutorial but many of methods are deprecated so it's not working EX.我正在尝试使用本教程来实现,但许多方法已被弃用,因此它不适用于 EX。
simpleExoplayer = ExoPlayerFactory.newSimpleInstance(
DefaultRenderersFactory(this),
DefaultTrackSelector(adaptiveTrackSelectionFactory),
DefaultLoadControl()
)
can anyone suggest me where to start or how do i build music streaming app using latest version of exoplayer
.任何人都可以建议我从哪里开始或如何使用最新版本的
exoplayer
构建音乐流媒体应用程序。
Any help would be highly appreciated.任何帮助将不胜感激。
With the new update, you can create a simple player instance using SimpleExoPlayer.Builder
:通过新的更新,您可以使用
SimpleExoPlayer.Builder
创建一个简单的播放器实例:
simpleExoplayer = SimpleExoPlayer.Builder(context).build()
You can also supply the Builder
with different arguments.您还可以为
Builder
提供不同的参数。 See https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html请参阅https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html
You can use this simple custom class I created to help you get started.您可以使用我创建的这个简单的自定义类来帮助您入门。
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
}
}
}
I also faced this problem and this my solution我也遇到了这个问题,这是我的解决方案
private val exoPlayer: SimpleExoPlayer by lazy { SimpleExoPlayer.Builder(this).build()}
Play Song播放歌曲
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()
}
to set listener of player and notification设置播放器和通知的侦听器
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")
}
}
})
}
to Kill player杀死玩家
private fun onDestroy() {
if (exoPlayer != null) {
exoPlayer.release()
exoPlayer = null
mediaSource = null
}
}
for more detail you can see official documentation更详细的可以看官方文档
The Universal Music Player uses ExoPlayer for local audio playback.通用音乐播放器使用 ExoPlayer 进行本地音频播放。
Building feature-rich media apps with ExoPlayer (Google I/O '18)使用 ExoPlayer 构建功能丰富的媒体应用程序 (Google I/O '18)
For Java Guys (Long Live Java)对于 Java 人(Java 万岁)
In Activity活动中
private PlayerView epPlayerView = findViewById(R.id.design_reference);
The Public Function公共职能
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.addMediaItem(mediaItem);
exoPlayer.prepare();
exoPlayer.setPlayWhenReady(true);
}
Build 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'
Until 2.15.0 version, you can create SimpleExoPlayer instance as the following :直到 2.15.0 版本,您可以创建 SimpleExoPlayer 实例如下:
SimpleExoPlayer.Builder(this)
.setMediaSourceFactory(mediaSourceFactory)
.build()
With the 2.16.0 version, SimpleExoPlayer is deprecated, you should use ExoPlayer instance instead.在 2.16.0 版本中,不推荐使用 SimpleExoPlayer,您应该使用 ExoPlayer 实例代替。 You can create it as the following :
您可以按以下方式创建它:
ExoPlayer.Builder(this)
.setMediaSourceFactory(mediaSourceFactory)
.build()
https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.