Skip to content

Commit

Permalink
Fixed loading cover in visualizer #2696
Browse files Browse the repository at this point in the history
  • Loading branch information
fast4x committed Jul 9, 2024
1 parent c50a9ba commit c7d37d2
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.MUSIC_PLAYER" />
<action android:name="android.intent.action.MEDIA_BUTTON" />

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
Expand Down
86 changes: 83 additions & 3 deletions app/src/main/kotlin/it/fast4x/rimusic/service/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
import android.content.pm.ServiceInfo
import android.content.res.Configuration
import android.database.SQLException
import android.graphics.Bitmap
Expand All @@ -21,6 +22,7 @@ import android.media.audiofx.AudioEffect
import android.media.audiofx.LoudnessEnhancer
import android.media.session.PlaybackState
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.support.v4.media.MediaDescriptionCompat
Expand All @@ -29,13 +31,15 @@ import android.support.v4.media.RatingCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import android.text.format.DateUtils
import android.view.KeyEvent
import androidx.annotation.OptIn
import androidx.compose.runtime.MutableFloatState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.startForegroundService
import androidx.core.content.getSystemService
Expand Down Expand Up @@ -957,7 +961,19 @@ class PlayerService : InvincibleService(),
Timber.e("maybeRestoreFromDiskPlayerQueue PlayerService startForegroundService ${it.stackTraceToString()}")
}
runCatching {
startForeground(NotificationId, notification())
//startForeground(NotificationId, notification())
notification()?.let {
ServiceCompat.startForeground(
this@PlayerService,
NotificationId,
it,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
} else {
0
}
)
}
}.onFailure {
Timber.e("maybeRestoreFromDiskPlayerQueue PlayerService startForeground ${it.stackTraceToString()}")
}
Expand Down Expand Up @@ -1070,7 +1086,19 @@ class PlayerService : InvincibleService(),
Timber.e("maybeRestorePlayerQueue startForegroundService ${it.stackTraceToString()}")
}
runCatching {
startForeground(NotificationId, notification())
//startForeground(NotificationId, notification())
notification()?.let {
ServiceCompat.startForeground(
this@PlayerService,
NotificationId,
it,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
} else {
0
}
)
}
}.onFailure {
Timber.e("maybeRestorePlayerQueue startForeground ${it.stackTraceToString()}")
}
Expand Down Expand Up @@ -1285,7 +1313,19 @@ class PlayerService : InvincibleService(),
Timber.e("Failed startForegroundService in PlayerService onEvents ${it.stackTraceToString()}")
}
kotlin.runCatching {
startForeground(NotificationId, notification)
//startForeground(NotificationId, notification)
notification()?.let {
ServiceCompat.startForeground(
this@PlayerService,
NotificationId,
it,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
} else {
0
}
)
}
}.onFailure {
Timber.e("Failed startForeground in PlayerService onEvents ${it.stackTraceToString()}")
}
Expand Down Expand Up @@ -2198,6 +2238,46 @@ class PlayerService : InvincibleService(),
binder.playFromSearch(query)
}

override fun onMediaButtonEvent(mediaButtonEvent: Intent?): Boolean {
mediaButtonEvent?.let {
if (it.action == Intent.ACTION_MEDIA_BUTTON) {
if (it.extras?.getBoolean(Intent.EXTRA_KEY_EVENT) == true) {
val keyEvent = it.extras?.getParcelable<KeyEvent>(Intent.EXTRA_KEY_EVENT)
when(keyEvent?.keyCode) {
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> {
if (player.isPlaying)
onPause()
else onPlay()

return true
}
KeyEvent.KEYCODE_MEDIA_NEXT -> {
onSkipToNext()
return true
}
KeyEvent.KEYCODE_MEDIA_PREVIOUS -> {
onSkipToPrevious()
return true
}
KeyEvent.KEYCODE_MEDIA_STOP -> {
onStop()
return true
}
KeyEvent.KEYCODE_MEDIA_PLAY -> {
onPlay()
return true
}
KeyEvent.KEYCODE_MEDIA_PAUSE -> {
onPause()
return true
}
}
}
}
}
return super.onMediaButtonEvent(mediaButtonEvent)
}

}


Expand Down
27 changes: 25 additions & 2 deletions app/src/main/kotlin/it/fast4x/rimusic/utils/InvincibleService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ServiceInfo
import android.os.Binder
import android.os.Build
import android.os.Handler
import android.os.Looper
import androidx.core.app.ServiceCompat
import timber.log.Timber

// https://stackoverflow.com/q/53502244/16885569
Expand Down Expand Up @@ -78,7 +81,17 @@ abstract class InvincibleService : Service() {
Intent.ACTION_SCREEN_OFF -> notification()?.let { notification ->
handler.removeCallbacks(this)
runCatching {
startForeground(notificationId, notification)
//startForeground(notificationId, notification)
ServiceCompat.startForeground(
this@InvincibleService,
notificationId,
notification,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
} else {
0
}
)
}.onFailure {
Timber.e("Failed startForeground in InvincibleService onReceive ${it.stackTraceToString()}")
}
Expand Down Expand Up @@ -111,7 +124,17 @@ abstract class InvincibleService : Service() {
if (shouldBeInvincible() && isAllowedToStartForegroundServices) {
notification()?.let { notification ->
runCatching {
startForeground(notificationId, notification)
//startForeground(notificationId, notification)
ServiceCompat.startForeground(
this@InvincibleService,
notificationId,
notification,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
} else {
0
}
)
}.onFailure {
Timber.e("Failed startForeground in InvincibleService run ${it.stackTraceToString()}")
}
Expand Down

0 comments on commit c7d37d2

Please sign in to comment.