Skip to content

Commit

Permalink
Merge pull request #787 from ShadyBoshra2012/master
Browse files Browse the repository at this point in the history
Fix Android 13 Actions icon disappeared
  • Loading branch information
kalismeras61 authored Aug 11, 2023
2 parents 44d28ec + ae91b5c commit ec27932
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 54 deletions.
12 changes: 9 additions & 3 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
<application>
<receiver
android:name=".notification.NotificationActionReceiver"
android:exported="true" />
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<receiver
android:name=".notification.CustomMediaButtonReceiver"
android:exported="true">
Expand All @@ -19,12 +23,14 @@
<service
android:name=".notification.NotificationService"
android:enabled="true"
android:foregroundServiceType="mediaPlayback"
android:exported="true">
<!--
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
-->
</service>

</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import kotlinx.coroutines.launch
import kotlin.math.abs
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.support.v4.media.session.MediaSessionCompat
import com.github.florent37.assets_audio_player.AssetsAudioPlayerPlugin

class NotificationService : Service() {

Expand Down Expand Up @@ -59,7 +61,7 @@ class NotificationService : Service() {
MediaButtonsReceiver.getMediaSessionCompat(context).let { mediaSession ->
val state = if (isPlaying) PlaybackStateCompat.STATE_PLAYING else PlaybackStateCompat.STATE_PAUSED
val newState = PlaybackStateCompat.Builder()
.setActions(ACTION_SEEK_TO)
.setActions(PlaybackStateCompat.ACTION_PLAY or PlaybackStateCompat.ACTION_PLAY_PAUSE or PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS or PlaybackStateCompat.ACTION_SKIP_TO_NEXT or PlaybackStateCompat.ACTION_SEEK_TO)
.setState(state, currentPositionMs, if (isPlaying) speed else 0f)
.build()

Expand Down Expand Up @@ -235,11 +237,48 @@ class NotificationService : Service() {

val context = this

val player = AssetsAudioPlayerPlugin.instance?.assetsAudioPlayer?.getPlayer(action.playerId) ?: return

val callback = object: MediaSessionCompat.Callback() {
override fun onPlay() {
player.askPlayOrPause()
}

override fun onPause() {
player.askPlayOrPause()
}

override fun onSkipToPrevious() {
player.prev()
}

override fun onSkipToNext() {
player.next()
}

override fun onSeekTo(pos: Long) {
player.seek(pos)
}

//override fun onCustomAction(action: String, extras: Bundle?) {
//when (action) {
// CUSTOM_ACTION_1 -> doCustomAction1(extras)
// CUSTOM_ACTION_2 -> doCustomAction2(extras)
// else -> {
// Log.w(TAG, "Unknown custom action $action")
// }
//}
//}

}

mediaSession.setCallback(callback)

val notification = NotificationCompat.Builder(this, CHANNEL_ID)
//prev
.apply {
if (notificationSettings.prevEnabled) {
addAction(getPrevIcon(context, action.notificationSettings.previousIcon), "prev",
addAction(getPrevIcon(context, action.notificationSettings.previousIcon), "Previous",
PendingIntent.getBroadcast(context, 0, createReturnIntent(forAction = NotificationAction.ACTION_PREV, forPlayer = action.playerId, audioMetas = action.audioMetas), FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)
)
}
Expand All @@ -249,23 +288,23 @@ class NotificationService : Service() {
if (notificationSettings.playPauseEnabled) {
addAction(
if (action.isPlaying) getPauseIcon(context, action.notificationSettings.pauseIcon) else getPlayIcon(context, action.notificationSettings.playIcon),
if (action.isPlaying) "pause" else "play",
if (action.isPlaying) "Pause" else "Play",
pendingToggleIntent
)
}
}
//next
.apply {
if (notificationSettings.nextEnabled) {
addAction(getNextIcon(context, action.notificationSettings.nextIcon), "next", PendingIntent.getBroadcast(context, 0,
addAction(getNextIcon(context, action.notificationSettings.nextIcon), "Next", PendingIntent.getBroadcast(context, 0,
createReturnIntent(forAction = NotificationAction.ACTION_NEXT, forPlayer = action.playerId, audioMetas = action.audioMetas), FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)
)
}
}
//stop
.apply {
if (notificationSettings.stopEnabled) {
addAction(getStopIcon(context, action.notificationSettings.stopIcon), "stop", PendingIntent.getBroadcast(context, 0,
addAction(getStopIcon(context, action.notificationSettings.stopIcon), "Stop", PendingIntent.getBroadcast(context, 0,
createReturnIntent(forAction = NotificationAction.ACTION_STOP, forPlayer = action.playerId, audioMetas = action.audioMetas), FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)
)
}
Expand Down
Loading

0 comments on commit ec27932

Please sign in to comment.