I have been trying to play audio using exoplayer media3 lib in background. So, far I was able to play the audio and it is playing in background too but I have kept url to be a test url which is static. Now, I want to pass the url to my service class which I am not able to understand how to do.
Attaching my code files here..
class PlaybackService : MediaSessionService() {
// Create your Player and MediaSession in the onCreate lifecycle event
lateinit var player: Player
private var mediaSession: MediaSession? = null
override fun onCreate() {
super.onCreate()
player = ExoPlayer.Builder(this).build()
mediaSession = MediaSession.Builder(this, player).build()
player.prepare()
player.playWhenReady = true
player.seekTo(0, 0)
val mediaItem =
MediaItem.fromUri("https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")
player.setMediaItem(mediaItem)
}
// Return a MediaSession to link with the MediaController that is making
// this request.
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? =
mediaSession
override fun onDestroy() {
mediaSession?.run {
player.release()
release()
mediaSession = null
}
}
}
Here is my fragment
class MusicPlayerFragment : BaseFragment() {
private lateinit var controllerFuture: ListenableFuture<MediaController>
private val controller: MediaController?
get() = if (controllerFuture.isDone) controllerFuture.get() else null
private val currentWindow = 0
var playerStopped = true
private val playbackPosition: Long = 0
override fun onStart() {
super.onStart()
playMedia()
}
private fun playMedia() {
val sessionToken = SessionToken(
requireContext(),
ComponentName(requireContext(), PlaybackService::class.java)
)
controllerFuture =
MediaController.Builder(requireContext(), sessionToken)
.buildAsync()
controllerFuture.addListener({
fragmentMusicPlayerBinding.playerView.player = controllerFuture.get()
fragmentMusicPlayerBinding.controls.player = controllerFuture.get()
}, MoreExecutors.directExecutor())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
fragmentMusicPlayerBinding.imgPlay.setOnClickListener {
if (playerStopped) {
playerStopped = false
playMedia()
}
}
fragmentMusicPlayerBinding.imgStop.setOnClickListener {
stopMediaPlayer()
}
fragmentMusicPlayerBinding.imgPause.setOnClickListener {
fragmentMusicPlayerBinding.playerView.player?.pause()
}
fragmentMusicPlayerBinding.imgRewind.setOnClickListener {
fragmentMusicPlayerBinding.playerView.player?.seekTo(0)
}
}
private fun stopMediaPlayer() {
controllerFuture.get().stop()
controllerFuture.get().playWhenReady = false
MediaController.releaseFuture(controllerFuture)
playerStopped = true
fragmentMusicPlayerBinding.playerView.player = null
}
}
Problems I am facing
I want to set media uri dynamically which I will get from api. So, how to do that?
Using this code I get a mediapplayer notification in my notification center, when I play or pause from there music get stops or play but at the same time when I stop from notification and play from fragment music doesn't plays.
Whenever I stop music from fragment, then too music keeps playing in background and notification is still there
When I clear out the music notification from the notification center then also music keeps playing.
Please help me out with the solutions to the problem I facing if anyone have faced any. TIA