i am trying to implement exoplayer this is my exoplayer
version
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. i couldn't understand anything in the official documentation .
i find many example and tutorial but it's all about playing video using 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.
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
.
Any help would be highly appreciated.
With the new update, you can create a simple player instance using SimpleExoPlayer.Builder
:
simpleExoplayer = SimpleExoPlayer.Builder(context).build()
You can also supply the Builder
with different arguments. See 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
}
}
}