I am new to dagger hilt, but it has been straight to familiarize myself. I am building a Music player application, using Media 3 exoplayer, However, recently I have observed that the app works fine on first launch, but breaks on subsequent launches if the app is closed (removed from recent apps). Here is how my dagger di is defined
@Module
@InstallIn(SingletonComponent::class)
class AppModule {
@Provides
@Singleton
fun providesAudioAttributes(): AudioAttributes = AudioAttributes.Builder()
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.setUsage(C.USAGE_MEDIA)
.build()
@OptIn(UnstableApi::class)
@Provides
@Singleton
fun provideExoPlayer(
@ApplicationContext context: Context,
audioAttributes: AudioAttributes,
): ExoPlayer {
Log.d("ExoPlayer", "Creating new instance of ExoPlayer")
return ExoPlayer.Builder(context)
.setAudioAttributes(audioAttributes, true)
.setHandleAudioBecomingNoisy(true)
.setTrackSelector(DefaultTrackSelector(context))
.build()
}
I also see following error messages on logcat
Handler (android.os.Handler) {63c3727} sending message to a Handler on a dead thread (Ask Gemini)
java.lang.IllegalStateException: Handler (android.os.Handler) {63c3727} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:560)
at android.os.Handler.enqueueMessage(Handler.java:786)
at android.os.Handler.sendMessageAtTime(Handler.java:735)
at android.os.Handler.sendMessageDelayed(Handler.java:705)
at android.os.Handler.sendMessage(Handler.java:643)
NB: The log Creating new instance of ExoPlayer
is only seen on app fresh launches, but on subsequent launches, its not visible, meaning its not instantiated again, which it wont work as I had already cleared
@AndroidEntryPoint
class EPlayerSessionService:MediaSessionService() {
@Inject
lateinit var mediaSession: MediaSession
@Inject
lateinit var notificationManager: EPlayerNotificationManager
@Inject
lateinit var playerServiceHandler: PlayerServiceHandler
@Inject
lateinit var exoPlayer: ExoPlayer
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
// Handle cleanup when the task is removed
if (exoPlayer.playWhenReady) {
exoPlayer.pause()
}
exoPlayer.release()
mediaSession.release()
stopSelf()
}
}
What can I do to fix this?