Skip to content

Commit

Permalink
Merge pull request #1205 from indiefan/command-target-duplication-fix
Browse files Browse the repository at this point in the history
Fix bug iOS AudioPlayer where multiple calls to setupRemoteTransportC…
  • Loading branch information
advplyr authored May 17, 2024
2 parents 1a7ae46 + 984f01d commit 06a0a68
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ios/App/Shared/player/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ class AudioPlayer: NSObject {
let jumpBackwardsTime = deviceSettings.jumpBackwardsTime

commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.removeTarget(nil)
commandCenter.playCommand.addTarget { [weak self] event in
guard let strongSelf = self else { return .commandFailed }
if strongSelf.isPlaying() {
Expand All @@ -595,6 +596,7 @@ class AudioPlayer: NSObject {
}

commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.removeTarget(nil)
commandCenter.pauseCommand.addTarget { [weak self] event in
guard let strongSelf = self else { return .commandFailed }
if strongSelf.isPlaying() {
Expand All @@ -606,6 +608,7 @@ class AudioPlayer: NSObject {
}

commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.removeTarget(nil)
commandCenter.togglePlayPauseCommand.addTarget { [weak self] event in
guard let strongSelf = self else { return .commandFailed }
if strongSelf.isPlaying() {
Expand All @@ -617,6 +620,7 @@ class AudioPlayer: NSObject {
}

commandCenter.skipForwardCommand.isEnabled = true
commandCenter.skipForwardCommand.removeTarget(nil)
commandCenter.skipForwardCommand.preferredIntervals = [NSNumber(value: jumpForwardTime)]
commandCenter.skipForwardCommand.addTarget { [weak self] event in
guard let command = event.command as? MPSkipIntervalCommand else {
Expand All @@ -629,6 +633,7 @@ class AudioPlayer: NSObject {
return .success
}
commandCenter.skipBackwardCommand.isEnabled = true
commandCenter.skipBackwardCommand.removeTarget(nil)
commandCenter.skipBackwardCommand.preferredIntervals = [NSNumber(value: jumpBackwardsTime)]
commandCenter.skipBackwardCommand.addTarget { [weak self] event in
guard let command = event.command as? MPSkipIntervalCommand else {
Expand All @@ -642,6 +647,7 @@ class AudioPlayer: NSObject {
}

commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.removeTarget(nil)
commandCenter.nextTrackCommand.addTarget { [weak self] _ in
guard let currentTime = self?.getCurrentTime() else {
return .commandFailed
Expand All @@ -650,6 +656,7 @@ class AudioPlayer: NSObject {
return .success
}
commandCenter.previousTrackCommand.isEnabled = true
commandCenter.previousTrackCommand.removeTarget(nil)
commandCenter.previousTrackCommand.addTarget { [weak self] _ in
guard let currentTime = self?.getCurrentTime() else {
return .commandFailed
Expand All @@ -659,6 +666,7 @@ class AudioPlayer: NSObject {
}

commandCenter.changePlaybackPositionCommand.isEnabled = deviceSettings.allowSeekingOnMediaControls
commandCenter.changePlaybackPositionCommand.removeTarget(nil)
commandCenter.changePlaybackPositionCommand.addTarget { [weak self] event in
guard let event = event as? MPChangePlaybackPositionCommandEvent else {
return .noSuchContent
Expand All @@ -676,6 +684,7 @@ class AudioPlayer: NSObject {
}

commandCenter.changePlaybackRateCommand.isEnabled = true
commandCenter.changePlaybackRateCommand.removeTarget(nil)
commandCenter.changePlaybackRateCommand.supportedPlaybackRates = [0.5, 0.75, 1.0, 1.25, 1.5, 2]
commandCenter.changePlaybackRateCommand.addTarget { [weak self] event in
guard let event = event as? MPChangePlaybackRateCommandEvent else {
Expand Down

0 comments on commit 06a0a68

Please sign in to comment.