Fix:Starting cast player and stopping cast closing audio player in app #314

This commit is contained in:
advplyr 2022-08-09 17:35:42 -05:00
parent 56c73c12db
commit 7c3b6c9702
2 changed files with 9 additions and 12 deletions

View file

@ -29,9 +29,14 @@ class PlayerNotificationListener(var playerNotificationService:PlayerNotificatio
Log.d(tag, "onNotificationCancelled not dismissed by user") Log.d(tag, "onNotificationCancelled not dismissed by user")
// When stop button is pressed on the notification I guess it isn't considered "dismissedByUser" so we need to close playback ourselves // When stop button is pressed on the notification I guess it isn't considered "dismissedByUser" so we need to close playback ourselves
if (!PlayerNotificationService.isClosed) { if (!PlayerNotificationService.isClosed && !PlayerNotificationService.isSwitchingPlayer) {
Log.d(tag, "PNS is not closed - closing it now") Log.d(tag, "PNS is not closed - closing it now")
playerNotificationService.closePlayback() playerNotificationService.closePlayback()
} else if (PlayerNotificationService.isSwitchingPlayer) {
// When switching from cast player to exo player and vice versa the notification is cancelled and posted again
// so we don't want to cancel the playback during this switch
Log.d(tag, "PNS is switching player")
PlayerNotificationService.isSwitchingPlayer = false
} }
} }
} }

View file

@ -50,6 +50,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
var isStarted = false var isStarted = false
var isClosed = false var isClosed = false
var isUnmeteredNetwork = false var isUnmeteredNetwork = false
var isSwitchingPlayer = false // Used when switching between cast player and exoplayer
} }
interface ClientEventEmitter { interface ClientEventEmitter {
@ -309,8 +310,6 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
val seekBackTime = DeviceManager.deviceData.deviceSettings?.jumpBackwardsTimeMs ?: 10000 val seekBackTime = DeviceManager.deviceData.deviceSettings?.jumpBackwardsTimeMs ?: 10000
val seekForwardTime = DeviceManager.deviceData.deviceSettings?.jumpForwardTimeMs ?: 10000 val seekForwardTime = DeviceManager.deviceData.deviceSettings?.jumpForwardTimeMs ?: 10000
Log.d(tag, "Seek Back Time $seekBackTime")
Log.d(tag, "Seek Forward Time $seekForwardTime")
mPlayer = ExoPlayer.Builder(this) mPlayer = ExoPlayer.Builder(this)
.setLoadControl(customLoadControl) .setLoadControl(customLoadControl)
@ -413,7 +412,6 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
currentPlayer.setPlaybackSpeed(playbackRateToUse) currentPlayer.setPlaybackSpeed(playbackRateToUse)
currentPlayer.prepare() currentPlayer.prepare()
} else if (castPlayer != null) { } else if (castPlayer != null) {
val currentTrackIndex = playbackSession.getCurrentTrackIndex() val currentTrackIndex = playbackSession.getCurrentTrackIndex()
val currentTrackTime = playbackSession.getCurrentTrackTimeMs() val currentTrackTime = playbackSession.getCurrentTrackTimeMs()
@ -477,6 +475,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
return return
} else { } else {
Log.d(tag, "switchToPlayer: Switching to cast player from exo player stop exo player") Log.d(tag, "switchToPlayer: Switching to cast player from exo player stop exo player")
isSwitchingPlayer = true
mPlayer.stop() mPlayer.stop()
} }
} else { } else {
@ -485,6 +484,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
return return
} else if (castPlayer != null) { } else if (castPlayer != null) {
Log.d(tag, "switchToPlayer: Switching to exo player from cast player stop cast player") Log.d(tag, "switchToPlayer: Switching to exo player from cast player stop cast player")
isSwitchingPlayer = true
castPlayer?.stop() castPlayer?.stop()
} }
} }
@ -548,10 +548,6 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
return currentPlaybackSession?.totalDurationMs ?: 0L return currentPlaybackSession?.totalDurationMs ?: 0L
} }
fun getCurrentBookTitle() : String? {
return currentPlaybackSession?.displayTitle
}
fun getCurrentPlaybackSessionCopy() :PlaybackSession? { fun getCurrentPlaybackSessionCopy() :PlaybackSession? {
return currentPlaybackSession?.clone() return currentPlaybackSession?.clone()
} }
@ -656,10 +652,6 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
return return
} }
currentPlayer.volume = 1F currentPlayer.volume = 1F
if (currentPlayer == castPlayer) {
Log.d(tag, "CAST Player set on play ${currentPlayer.isLoading} || ${currentPlayer.duration} | ${currentPlayer.currentPosition}")
}
currentPlayer.play() currentPlayer.play()
} }