2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.playback
|
2020-07-10 17:18:29 +02:00
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
Fix Bluetooth control button unresponsiveness.
With Oreo and later, Bluetooth control buttons may kill FFA if it is
not the foreground application. Once this happens to resume playback,
one needs to restart playback from the phone, rather than the
play/pause action of Bluetooth headset.
For example:
D MediaSessionService: Sending KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_PLAY, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.dev (
W ActivityManager: Background start not allowed: service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.play
549 uid=10149 pkg=audio.funkwhale.ffa.dev startFg?=false
D AndroidRuntime: Shutting down VM
--------- beginning of crash
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: audio.funkwhale.ffa.dev, PID: 14549
E AndroidRuntime: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) }: app is in background uid UidRecord{72fa8f8 u0a149 CAC bg:+11m56s597ms idle change:cached procs:1 seq(0,0,0)}
E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1532)
E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:664)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.invoke$lambda-3$lambda-2(MediaSession.kt:47)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.$r8$lambda$jU84j_zRyeYuvwLrRY0b6XyQBMs(Unknown Source:0)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2$$ExternalSyntheticLambda0.onMediaButtonEvent(Unknown Source:2)
E AndroidRuntime: at com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector$ComponentListener.onMediaButtonEvent(MediaSessionConnector.java:1396)
E AndroidRuntime: at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.onMediaButtonEvent(MediaSessionCompat.java:1602)
E AndroidRuntime: at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1471)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6718)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
W ActivityManager: Force finishing activity audio.funkwhale.ffa.dev/audio.funkwhale.ffa.activities.MainActivity
xref: https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten
2022-06-04 02:10:03 +02:00
|
|
|
import android.os.Build
|
2020-07-10 17:18:29 +02:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.ResultReceiver
|
|
|
|
import android.support.v4.media.session.MediaSessionCompat
|
|
|
|
import android.support.v4.media.session.PlaybackStateCompat
|
2021-07-12 10:14:26 +02:00
|
|
|
import audio.funkwhale.ffa.utils.Command
|
|
|
|
import audio.funkwhale.ffa.utils.CommandBus
|
2020-07-10 17:18:29 +02:00
|
|
|
import com.google.android.exoplayer2.ControlDispatcher
|
|
|
|
import com.google.android.exoplayer2.Player
|
|
|
|
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
|
|
|
|
|
|
|
class MediaSession(private val context: Context) {
|
2021-08-09 07:03:20 +02:00
|
|
|
|
2020-07-12 18:28:50 +02:00
|
|
|
var active = false
|
2020-07-10 17:18:29 +02:00
|
|
|
|
|
|
|
private val playbackStateBuilder = PlaybackStateCompat.Builder().apply {
|
|
|
|
setActions(
|
|
|
|
PlaybackStateCompat.ACTION_PLAY_PAUSE or
|
|
|
|
PlaybackStateCompat.ACTION_PLAY or
|
|
|
|
PlaybackStateCompat.ACTION_PAUSE or
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_NEXT or
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS or
|
|
|
|
PlaybackStateCompat.ACTION_SEEK_TO or
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
val session: MediaSessionCompat by lazy {
|
|
|
|
MediaSessionCompat(context, context.packageName).apply {
|
|
|
|
setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
|
|
|
|
setPlaybackState(playbackStateBuilder.build())
|
|
|
|
|
|
|
|
isActive = true
|
2020-07-12 18:28:50 +02:00
|
|
|
active = true
|
2020-07-10 17:18:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val connector: MediaSessionConnector by lazy {
|
2020-07-12 18:28:50 +02:00
|
|
|
MediaSessionConnector(session).also {
|
2021-08-22 09:48:33 +02:00
|
|
|
it.setQueueNavigator(FFAQueueNavigator())
|
2020-07-10 17:18:29 +02:00
|
|
|
|
2020-07-12 18:28:50 +02:00
|
|
|
it.setMediaButtonEventHandler { _, _, intent ->
|
2020-07-10 17:18:29 +02:00
|
|
|
if (!active) {
|
Fix Bluetooth control button unresponsiveness.
With Oreo and later, Bluetooth control buttons may kill FFA if it is
not the foreground application. Once this happens to resume playback,
one needs to restart playback from the phone, rather than the
play/pause action of Bluetooth headset.
For example:
D MediaSessionService: Sending KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_PLAY, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.dev (
W ActivityManager: Background start not allowed: service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.play
549 uid=10149 pkg=audio.funkwhale.ffa.dev startFg?=false
D AndroidRuntime: Shutting down VM
--------- beginning of crash
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: audio.funkwhale.ffa.dev, PID: 14549
E AndroidRuntime: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) }: app is in background uid UidRecord{72fa8f8 u0a149 CAC bg:+11m56s597ms idle change:cached procs:1 seq(0,0,0)}
E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1532)
E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:664)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.invoke$lambda-3$lambda-2(MediaSession.kt:47)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.$r8$lambda$jU84j_zRyeYuvwLrRY0b6XyQBMs(Unknown Source:0)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2$$ExternalSyntheticLambda0.onMediaButtonEvent(Unknown Source:2)
E AndroidRuntime: at com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector$ComponentListener.onMediaButtonEvent(MediaSessionConnector.java:1396)
E AndroidRuntime: at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.onMediaButtonEvent(MediaSessionCompat.java:1602)
E AndroidRuntime: at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1471)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6718)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
W ActivityManager: Force finishing activity audio.funkwhale.ffa.dev/audio.funkwhale.ffa.activities.MainActivity
xref: https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten
2022-06-04 02:10:03 +02:00
|
|
|
Intent(context, PlayerService::class.java).let { player ->
|
|
|
|
player.action = intent.action
|
2020-07-10 17:18:29 +02:00
|
|
|
|
Fix Bluetooth control button unresponsiveness.
With Oreo and later, Bluetooth control buttons may kill FFA if it is
not the foreground application. Once this happens to resume playback,
one needs to restart playback from the phone, rather than the
play/pause action of Bluetooth headset.
For example:
D MediaSessionService: Sending KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_PLAY, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.dev (
W ActivityManager: Background start not allowed: service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.play
549 uid=10149 pkg=audio.funkwhale.ffa.dev startFg?=false
D AndroidRuntime: Shutting down VM
--------- beginning of crash
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: audio.funkwhale.ffa.dev, PID: 14549
E AndroidRuntime: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) }: app is in background uid UidRecord{72fa8f8 u0a149 CAC bg:+11m56s597ms idle change:cached procs:1 seq(0,0,0)}
E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1532)
E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:664)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.invoke$lambda-3$lambda-2(MediaSession.kt:47)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.$r8$lambda$jU84j_zRyeYuvwLrRY0b6XyQBMs(Unknown Source:0)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2$$ExternalSyntheticLambda0.onMediaButtonEvent(Unknown Source:2)
E AndroidRuntime: at com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector$ComponentListener.onMediaButtonEvent(MediaSessionConnector.java:1396)
E AndroidRuntime: at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.onMediaButtonEvent(MediaSessionCompat.java:1602)
E AndroidRuntime: at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1471)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6718)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
W ActivityManager: Force finishing activity audio.funkwhale.ffa.dev/audio.funkwhale.ffa.activities.MainActivity
xref: https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten
2022-06-04 02:10:03 +02:00
|
|
|
intent.extras?.let { extras -> player.putExtras(extras) }
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
context.startForegroundService(player)
|
|
|
|
} else {
|
|
|
|
context.startService(player)
|
2021-09-09 09:56:15 +02:00
|
|
|
}
|
Fix Bluetooth control button unresponsiveness.
With Oreo and later, Bluetooth control buttons may kill FFA if it is
not the foreground application. Once this happens to resume playback,
one needs to restart playback from the phone, rather than the
play/pause action of Bluetooth headset.
For example:
D MediaSessionService: Sending KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_PLAY, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=0, downTime=0, deviceId=-1, source=0x0 } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.dev (
W ActivityManager: Background start not allowed: service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) } to audio.funkwhale.ffa.dev/audio.funkwhale.ffa.play
549 uid=10149 pkg=audio.funkwhale.ffa.dev startFg?=false
D AndroidRuntime: Shutting down VM
--------- beginning of crash
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: audio.funkwhale.ffa.dev, PID: 14549
E AndroidRuntime: java.lang.IllegalStateException: Not allowed to start service Intent { act=android.intent.action.MEDIA_BUTTON cmp=audio.funkwhale.ffa.dev/audio.funkwhale.ffa.playback.PlayerService (has extras) }: app is in background uid UidRecord{72fa8f8 u0a149 CAC bg:+11m56s597ms idle change:cached procs:1 seq(0,0,0)}
E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1532)
E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:664)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.invoke$lambda-3$lambda-2(MediaSession.kt:47)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2.$r8$lambda$jU84j_zRyeYuvwLrRY0b6XyQBMs(Unknown Source:0)
E AndroidRuntime: at audio.funkwhale.ffa.playback.MediaSession$connector$2$$ExternalSyntheticLambda0.onMediaButtonEvent(Unknown Source:2)
E AndroidRuntime: at com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector$ComponentListener.onMediaButtonEvent(MediaSessionConnector.java:1396)
E AndroidRuntime: at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.onMediaButtonEvent(MediaSessionCompat.java:1602)
E AndroidRuntime: at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1471)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6718)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
W ActivityManager: Force finishing activity audio.funkwhale.ffa.dev/audio.funkwhale.ffa.activities.MainActivity
xref: https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten
2022-06-04 02:10:03 +02:00
|
|
|
}
|
2020-07-10 17:18:29 +02:00
|
|
|
|
2020-07-12 18:28:50 +02:00
|
|
|
return@setMediaButtonEventHandler true
|
2020-07-10 17:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 09:48:33 +02:00
|
|
|
class FFAQueueNavigator : MediaSessionConnector.QueueNavigator {
|
2020-07-10 17:18:29 +02:00
|
|
|
override fun onSkipToQueueItem(player: Player, controlDispatcher: ControlDispatcher, id: Long) {
|
|
|
|
CommandBus.send(Command.PlayTrack(id.toInt()))
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCurrentWindowIndexChanged(player: Player) {}
|
|
|
|
|
|
|
|
override fun onCommand(player: Player, controlDispatcher: ControlDispatcher, command: String, extras: Bundle?, cb: ResultReceiver?) = true
|
|
|
|
|
|
|
|
override fun getSupportedQueueNavigatorActions(player: Player): Long {
|
|
|
|
return PlaybackStateCompat.ACTION_PLAY_PAUSE or
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_NEXT or
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS or
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSkipToNext(player: Player, controlDispatcher: ControlDispatcher) {
|
|
|
|
CommandBus.send(Command.NextTrack)
|
|
|
|
}
|
|
|
|
|
2020-07-12 15:19:33 +02:00
|
|
|
override fun getActiveQueueItemId(player: Player?) = player?.currentWindowIndex?.toLong() ?: 0
|
2020-07-10 17:18:29 +02:00
|
|
|
|
|
|
|
override fun onSkipToPrevious(player: Player, controlDispatcher: ControlDispatcher) {
|
|
|
|
CommandBus.send(Command.PreviousTrack)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onTimelineChanged(player: Player) {}
|
|
|
|
}
|