Skip to content

Commit

Permalink
Persist rate accross player vs item
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Aug 12, 2021
1 parent 9c32a86 commit aeef676
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions SwiftAudioEx/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ public class AudioPlayer: AVPlayerWrapperDelegate {
set { _wrapper.isMuted = newValue }
}

private var _rate: Float = 1.0
public var rate: Float {
get { return wrapper.rate }
set { _wrapper.rate = newValue }
get { return _rate }
set {
_rate = newValue

// Only set the rate on the wrapper if it is already playing.
if _wrapper.rate > 0 {
_wrapper.rate = newValue
}
}
}

// MARK: - Init
Expand Down Expand Up @@ -311,7 +319,11 @@ public class AudioPlayer: AVPlayerWrapperDelegate {
updateNowPlayingPlaybackValues()
}
setTimePitchingAlgorithmForCurrentItem()
case .playing, .paused:
case .playing:
// When a track starts playing, reset the rate to the stored rate
self.rate = _rate;
fallthrough
case .paused:
if (automaticallyUpdateNowPlayingInfo) {
updateNowPlayingPlaybackValues()
}
Expand Down

0 comments on commit aeef676

Please sign in to comment.