Emit playback speed change event when Android Auto changes speed

This commit is contained in:
advplyr 2023-02-06 17:20:39 -06:00
parent 17a894f5ef
commit 405cd21c32
5 changed files with 25 additions and 8 deletions

View file

@ -70,21 +70,22 @@ class MediaManager(private var apiHandler: ApiHandler, var ctx: Context) {
if (userSettingsPref != null) { if (userSettingsPref != null) {
try { try {
val userSettings = JSObject(userSettingsPref) val userSettings = JSObject(userSettingsPref)
userSettings.put("playbackRate", newRate.toDouble()) userSettings.put("playbackRate", newRate.toDouble()) // TODO: Handle storing exact values? This actually comes out to something like 2.00000004432
sharedPrefEditor.putString("userSettings", userSettings.toString()) sharedPrefEditor.putString("userSettings", userSettings.toString())
sharedPrefEditor.commit() sharedPrefEditor.apply()
userSettingsPlaybackRate = newRate userSettingsPlaybackRate = newRate
Log.d(tag, "Saved userSettings JSON from Android Auto") Log.d(tag, "Saved userSettings JSON from Android Auto with playbackRate=$newRate")
} catch(je:JSONException) { } catch(je:JSONException) {
Log.e(tag, "Failed to save userSettings JSON ${je.localizedMessage}") Log.e(tag, "Failed to save userSettings JSON ${je.localizedMessage}")
} }
} else { } else {
// Not sure if this is the best place for this, but if a user has not changed any user settings in the app // Not sure if this is the best place for this, but if a user has not changed any user settings in the app
// the object will not exist yet, could be moved to a centralized place or created on first app load // the object will not exist yet, could be moved to a centralized place or created on first app load
var userSettings = JSONObject() val userSettings = JSONObject()
userSettings.put("playbackRate", newRate.toDouble()) userSettings.put("playbackRate", newRate.toDouble())
sharedPrefEditor.putString("userSettings", userSettings.toString()) sharedPrefEditor.putString("userSettings", userSettings.toString())
Log.d(tag, "Created and saved userSettings JSON from Android Auto") userSettingsPlaybackRate = newRate
Log.d(tag, "Created and saved userSettings JSON from Android Auto with playbackRate=$newRate")
} }
} }
} }

View file

@ -90,9 +90,9 @@ class MediaSessionCallback(var playerNotificationService:PlayerNotificationServi
private fun onChangeSpeed() { private fun onChangeSpeed() {
// cycle to next speed, only contains preset android app options, as each increment needs it's own icon // cycle to next speed, only contains preset android app options, as each increment needs it's own icon
var mediaManager = playerNotificationService.mediaManager val mediaManager = playerNotificationService.mediaManager
var currentSpeed = mediaManager.getSavedPlaybackRate() val currentSpeed = mediaManager.getSavedPlaybackRate()
var newSpeed = when (currentSpeed) { val newSpeed = when (currentSpeed) {
0.5f -> 1.0f 0.5f -> 1.0f
1.0f -> 1.2f 1.0f -> 1.2f
1.2f -> 1.5f 1.2f -> 1.5f
@ -104,6 +104,7 @@ class MediaSessionCallback(var playerNotificationService:PlayerNotificationServi
} }
mediaManager.setSavedPlaybackRate(newSpeed) mediaManager.setSavedPlaybackRate(newSpeed)
playerNotificationService.setPlaybackSpeed(newSpeed) playerNotificationService.setPlaybackSpeed(newSpeed)
playerNotificationService.clientEventEmitter?.onPlaybackSpeedChanged(newSpeed)
} }
override fun onPlayFromMediaId(mediaId: String?, extras: Bundle?) { override fun onPlayFromMediaId(mediaId: String?, extras: Bundle?) {

View file

@ -80,6 +80,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
fun onProgressSyncSuccess() fun onProgressSyncSuccess()
fun onNetworkMeteredChanged(isUnmetered:Boolean) fun onNetworkMeteredChanged(isUnmetered:Boolean)
fun onMediaItemHistoryUpdated(mediaItemHistory:MediaItemHistory) fun onMediaItemHistoryUpdated(mediaItemHistory:MediaItemHistory)
fun onPlaybackSpeedChanged(playbackSpeed:Float)
} }
private val binder = LocalBinder() private val binder = LocalBinder()
@ -1187,6 +1188,8 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
override fun getCustomAction(player: Player): PlaybackStateCompat.CustomAction? { override fun getCustomAction(player: Player): PlaybackStateCompat.CustomAction? {
val playbackRate = mediaManager.getSavedPlaybackRate() val playbackRate = mediaManager.getSavedPlaybackRate()
// TODO: Handle custom playback rates like 0.7, 1.3, 1.4, etc
val drawable: Int = when (playbackRate) { val drawable: Int = when (playbackRate) {
0.5f -> R.drawable.ic_play_speed_0_5x 0.5f -> R.drawable.ic_play_speed_0_5x
1.0f -> R.drawable.ic_play_speed_1_0x 1.0f -> R.drawable.ic_play_speed_1_0x

View file

@ -97,6 +97,10 @@ class AbsAudioPlayer : Plugin() {
override fun onMediaItemHistoryUpdated(mediaItemHistory:MediaItemHistory) { override fun onMediaItemHistoryUpdated(mediaItemHistory:MediaItemHistory) {
notifyListeners("onMediaItemHistoryUpdated", JSObject(jacksonMapper.writeValueAsString(mediaItemHistory))) notifyListeners("onMediaItemHistoryUpdated", JSObject(jacksonMapper.writeValueAsString(mediaItemHistory)))
} }
override fun onPlaybackSpeedChanged(playbackSpeed:Float) {
emit("onPlaybackSpeedChanged", playbackSpeed)
}
}) })
MediaEventManager.clientEventEmitter = playerNotificationService.clientEventEmitter MediaEventManager.clientEventEmitter = playerNotificationService.clientEventEmitter

View file

@ -143,6 +143,7 @@ export default {
onMetadataListener: null, onMetadataListener: null,
onProgressSyncFailing: null, onProgressSyncFailing: null,
onProgressSyncSuccess: null, onProgressSyncSuccess: null,
onPlaybackSpeedChangedListener: null,
touchStartY: 0, touchStartY: 0,
touchStartTime: 0, touchStartTime: 0,
touchEndY: 0, touchEndY: 0,
@ -814,6 +815,11 @@ export default {
this.$toast.error(`Playback Failed: ${errorMessage}`) this.$toast.error(`Playback Failed: ${errorMessage}`)
this.endPlayback() this.endPlayback()
}, },
onPlaybackSpeedChanged(data) {
if (!data.value || isNaN(data.value)) return
this.currentPlaybackRate = Number(data.value)
this.updateTimestamp()
},
async init() { async init() {
this.useChapterTrack = await this.$localStore.getUseChapterTrack() this.useChapterTrack = await this.$localStore.getUseChapterTrack()
this.lockUi = await this.$localStore.getPlayerLock() this.lockUi = await this.$localStore.getPlayerLock()
@ -825,6 +831,7 @@ export default {
this.onMetadataListener = AbsAudioPlayer.addListener('onMetadata', this.onMetadata) this.onMetadataListener = AbsAudioPlayer.addListener('onMetadata', this.onMetadata)
this.onProgressSyncFailing = AbsAudioPlayer.addListener('onProgressSyncFailing', this.showProgressSyncIsFailing) this.onProgressSyncFailing = AbsAudioPlayer.addListener('onProgressSyncFailing', this.showProgressSyncIsFailing)
this.onProgressSyncSuccess = AbsAudioPlayer.addListener('onProgressSyncSuccess', this.showProgressSyncSuccess) this.onProgressSyncSuccess = AbsAudioPlayer.addListener('onProgressSyncSuccess', this.showProgressSyncSuccess)
this.onPlaybackSpeedChangedListener = AbsAudioPlayer.addListener('onPlaybackSpeedChanged', this.onPlaybackSpeedChanged)
}, },
screenOrientationChange() { screenOrientationChange() {
setTimeout(() => { setTimeout(() => {
@ -900,6 +907,7 @@ export default {
if (this.onPlaybackFailedListener) this.onPlaybackFailedListener.remove() if (this.onPlaybackFailedListener) this.onPlaybackFailedListener.remove()
if (this.onProgressSyncFailing) this.onProgressSyncFailing.remove() if (this.onProgressSyncFailing) this.onProgressSyncFailing.remove()
if (this.onProgressSyncSuccess) this.onProgressSyncSuccess.remove() if (this.onProgressSyncSuccess) this.onProgressSyncSuccess.remove()
if (this.onPlaybackSpeedChangedListener) this.onPlaybackSpeedChangedListener.remove()
clearInterval(this.playInterval) clearInterval(this.playInterval)
} }
} }