MediaBrowserServiceCompat not stopping correctly
@marcbaechinger is already working on this.
Since May 2, 2023.
- Dominant language
- Java
- Stars
- 3k
- Forks
- 955
- Avg merge
- 12d 14h
- Merged PRs (30d)
- 2
Description
Hi.
I am using MediaBrowserServiceCompat, ExoPlayer, and MediaBrowserCompat to connect to the service.
I followed the uamp project in regard how they release service.
I am stopping player onTaskRemoved (if i want service and audio to stop when app is closed). Which gets to onPlayerStateChanged and closes notification. There i am stopping foreground service, and call stopSelf().
Actually I tried most combinations of these lines of code on different steps:
exoPlayer.stop()
exoPlayer.clearMediaItems()
exoPlayer.removeListener(radioPlayerEventListener)
exoPlayer.release()
stopForeground(Service.STOP_FOREGROUND_REMOVE)
stopSelf()
radioNotificationManager.removeNotification()
mediaSession.run {
isActive = false
release()
}
serviceJob.cancel()
I also disconnect controller (otherwise service's onDestroy() won't be called)
fun disconnectBrowser(){
mediaController.unregisterCallback(mediaControllerCallback)
mediaBrowser.disconnect()
}
But service and application just does not stop on certain conditions.
- If i am closing an application while exoplayer plays audio (and i stop player on taskRemoved) service does not stop.
- If I close the app and don't stop player on taskRemoved, but instead use notification to stop player, and then remove notification, service also does not stop.
So service is destroyed correctly only in case when stopForeground(STOP_FOREGROUND_DETACH) is called before closing the app and exoplayer was paused before that.
I am using hilt injection for ExoPlayer instance:
@Module
@InstallIn(ServiceComponent::class)
object ServiceModule {
@Provides
@ServiceScoped
fun providesAudioAttributes() = AudioAttributes.Builder()
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.setUsage(C.USAGE_MEDIA)
.build()
@Provides
@ServiceScoped
fun providesExoPlayer (
@ApplicationContext app : Context,
audioAttributes: AudioAttributes,
renderersFactory: DefaultRenderersFactory
) = ExoPlayer.Builder(app, renderersFactory)
.setAudioAttributes(audioAttributes, true)
.setHandleAudioBecomingNoisy(true)
.build()
@Provides
@ServiceScoped
fun providesDataSourceFactory (
@ApplicationContext app : Context
) = DefaultDataSource.Factory(app)
@Provides
@ServiceScoped
fun providesRendersFactory(
@ApplicationContext app : Context,
exoRecord: ExoRecord
) = object : DefaultRenderersFactory(app){
override fun buildAudioSink(
context: Context,
enableFloatOutput: Boolean,
enableAudioTrackPlaybackParams: Boolean,
enableOffload: Boolean
): AudioSink {
return DefaultAudioSink.Builder()
.setAudioCapabilities(getCapabilities(app))
.setEnableFloatOutput(true)
.setEnableAudioTrackPlaybackParams(true)
.build()
}
}
}
And for MediaBrowserCompat :
@Provides
@Singleton
fun providesRadioServiceConnection(
@ApplicationContext app : Context
) = RadioServiceConnection(app)
The only working solution now seems to be just to kill process (put it in service's onDestroy():
android.os.Process.killProcess(android.os.Process.myPid())
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.