Skip to content

MediaBrowserServiceCompat not stopping correctly #370

Description

@MrBzik

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.

  1. If i am closing an application while exoplayer plays audio (and i stop player on taskRemoved) service does not stop.
  2. 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())

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions