diff --git a/app/src/main/java/com/dd3boh/outertune/MainActivity.kt b/app/src/main/java/com/dd3boh/outertune/MainActivity.kt index e62bba11f..1a0393e26 100644 --- a/app/src/main/java/com/dd3boh/outertune/MainActivity.kt +++ b/app/src/main/java/com/dd3boh/outertune/MainActivity.kt @@ -274,7 +274,7 @@ class MainActivity : ComponentActivity() { override fun onStart() { super.onStart() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - startService(Intent(this, MusicService::class.java)) + startForegroundService(Intent(this, MusicService::class.java)) } else { startService(Intent(this, MusicService::class.java)) } diff --git a/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt b/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt index 08bd4e001..eb6795f9f 100644 --- a/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt +++ b/app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt @@ -47,6 +47,7 @@ import androidx.media3.exoplayer.audio.DefaultAudioSink import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor import androidx.media3.exoplayer.source.DefaultMediaSourceFactory import androidx.media3.session.CommandButton +import androidx.media3.session.DefaultMediaNotificationProvider import androidx.media3.session.MediaController import androidx.media3.session.MediaLibraryService import androidx.media3.session.MediaSession @@ -192,14 +193,15 @@ class MusicService : MediaLibraryService(), override fun onCreate() { super.onCreate() - val notificationBuilder = NotificationCompat.Builder(this, KEEP_ALIVE_CHANNEL_ID) - .setContentTitle(getString(R.string.music_player)) - .setSmallIcon(R.drawable.small_icon) - .setOngoing(true) // Ensures notification stays until service stops - - val notification = notificationBuilder.build() + setMediaNotificationProvider( + DefaultMediaNotificationProvider(this, { NOTIFICATION_ID }, + CHANNEL_ID, R.string.music_player) + .apply { + setSmallIcon(R.drawable.small_icon) + } + ) - // FG notification + // FG keep alive if (dataStore.get(KeepAliveKey, false)) { try { startService(Intent(this, KeepAlive::class.java)) @@ -888,7 +890,7 @@ class MusicService : MediaLibraryService(), const val PLAYLIST = "playlist" const val CHANNEL_ID = "music_channel_01" - const val KEEP_ALIVE_CHANNEL_ID = "outertune_keep_alive" + const val NOTIFICATION_ID = 888 const val ERROR_CODE_NO_STREAM = 1000001 const val CHUNK_LENGTH = 512 * 1024L }