I have a media player application. I'm using MediaSessionService. In onCreate() i create Exoplayer instance.
mTrackSelector = DefaultTrackSelector(this)
mTrackSelector.parameters = mTrackSelector.parameters
.buildUpon()
.setPreferredAudioLanguage("en")
.setPreferredTextLanguage("en")
.build()
val mAudioAttr = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_SPEECH)
.build()
mPlayer = ExoPlayer.Builder(this)
.setSeekBackIncrementMs(30000)
.setSeekForwardIncrementMs(30000)
.setTrackSelector(mTrackSelector)
.setAudioAttributes(mAudioAttr, true)
.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT)
.build()
After that i'm creating MediaSession using ForwardingPlayer to adjust controls
mMediaSession = MediaSession.Builder(this, forwardingPlayer)
.setId(System.currentTimeMillis().toString().takeLast(5))
.setCallback(librarySessionCallback).build()
As far as i test it's all good for my player to run and post a media3 control notification. Wihout using user permission for notification or use push notification. I test it on android 8-14. Tested on android 13 in emulator
The problem is in production i receive throw Firebase Crashlitycs raports about :
Fatal Exception:
android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException
Context.startForegroundService() did not then call Service.startForeground():
ServiceRecord{4e0c02b u0 xx.xxxxx.PlayerService}
on android 13
I did not implement any MediaSessionServiceListener and override onForegroundServiceStartNotAllowedException because in my tests on android 13 and above on phones and emulator did not crash. For test i implement those but i still have no crash or any log in
override fun onForegroundServiceStartNotAllowedException() {
Log.d("testCrash", "*) ------> CRASH!! CRASH!!")
}
What i'm doing wrong? On my phones it did not crash How can i reproduce the crash ? onForegroundServiceStartNotAllowedException is not called on any android 13-14 of my phones
in gradle i have compileSdk 34 & targetSdk 34
I notice that YouTube premium does not ask for any notification permission and i can use audio in background on my android 13 and 14 phones
Thanks,
LE: i start my service using :
val mSessionToken = SessionToken(nInterface.getActivity(), ComponentName(nInterface.getActivity(), PlayerService::class.java))
My AndroidManifest :
<service android:name=".engine.notification.PlayerService"
android:foregroundServiceType="mediaPlayback"
android:stopWithTask="true"
android:exported="true">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService"/>
</intent-filter>
</service>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />