Skip to content

Commit

Permalink
player: Fix foreground service FGS crash
Browse files Browse the repository at this point in the history
* And fix duplicate notification
  • Loading branch information
mikooomich committed Jan 30, 2025
1 parent c8b17b9 commit becde1a
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
import android.database.SQLException
import android.media.audiofx.AudioEffect
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Binder
import android.os.Build
import android.widget.Toast
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.getSystemService
import androidx.core.net.toUri
import androidx.datastore.preferences.core.edit
Expand Down Expand Up @@ -61,9 +65,9 @@ import com.dd3boh.outertune.constants.AudioQualityKey
import com.dd3boh.outertune.constants.KeepAliveKey
import com.dd3boh.outertune.constants.LastPosKey
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleLike
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleStartRadio
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleRepeatMode
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleShuffle
import com.dd3boh.outertune.constants.MediaSessionConstants.CommandToggleStartRadio
import com.dd3boh.outertune.constants.PauseListenHistoryKey
import com.dd3boh.outertune.constants.PauseRemoteListenHistoryKey
import com.dd3boh.outertune.constants.PersistentQueueKey
Expand Down Expand Up @@ -95,8 +99,8 @@ import com.dd3boh.outertune.playback.queues.ListQueue
import com.dd3boh.outertune.playback.queues.Queue
import com.dd3boh.outertune.playback.queues.YouTubeQueue
import com.dd3boh.outertune.utils.CoilBitmapLoader
import com.dd3boh.outertune.utils.YTPlayerUtils
import com.dd3boh.outertune.utils.NetworkConnectivityObserver
import com.dd3boh.outertune.utils.YTPlayerUtils
import com.dd3boh.outertune.utils.dataStore
import com.dd3boh.outertune.utils.enumPreference
import com.dd3boh.outertune.utils.get
Expand Down Expand Up @@ -191,6 +195,7 @@ class MusicService : MediaLibraryService(),

lateinit var player: ExoPlayer
private lateinit var mediaSession: MediaLibrarySession
private lateinit var notificationManager: NotificationManagerCompat
private lateinit var playerNotificationManager: PlayerNotificationManager

private var isAudioEffectSessionOpened = false
Expand Down Expand Up @@ -378,14 +383,39 @@ class MusicService : MediaLibraryService(),
}
}

notificationManager = NotificationManagerCompat.from(this)
notificationManager.createNotificationChannel(
NotificationChannelCompat.Builder(
CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_HIGH
).apply {
setName(CHANNEL_NAME)
setLightsEnabled(false)
setShowBadge(false)
setSound(null, null)
}.build()
)

playerNotificationManager = PlayerNotificationManager.Builder(this, NOTIFICATION_ID, CHANNEL_ID)
.setNotificationListener(object : PlayerNotificationManager.NotificationListener {
override fun onNotificationPosted(notificationId: Int, notification: Notification, ongoing: Boolean) {
fun startFg() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(notificationId, notification, FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
} else {
startForeground(notificationId, notification)
}
}

// FG keep alive
if (dataStore.get(KeepAliveKey, false)) {
startForeground(notificationId, notification)
startFg()
} else {
stopForeground(notificationId)
// mimic media3 default behaviour
if (player.isPlaying) {
startFg()
} else {
stopForeground(notificationId)
}
}
}
})
Expand Down Expand Up @@ -902,10 +932,7 @@ class MusicService : MediaLibraryService(),
}

override fun onUpdateNotification(session: MediaSession, startInForegroundRequired: Boolean) {
// use default behaviour if keep alive is disabled
if (!dataStore.get(KeepAliveKey, false)) {
super.onUpdateNotification(session, startInForegroundRequired)
}
// we handle notification manually
}

override fun onDestroy() {
Expand Down Expand Up @@ -940,6 +967,7 @@ class MusicService : MediaLibraryService(),
const val PLAYLIST = "playlist"

const val CHANNEL_ID = "music_channel_01"
const val CHANNEL_NAME = "fgs_workaround"
const val NOTIFICATION_ID = 888
const val ERROR_CODE_NO_STREAM = 1000001
const val CHUNK_LENGTH = 512 * 1024L
Expand Down

0 comments on commit becde1a

Please sign in to comment.