diff --git a/Sources/SwiftFortuneWheel/SwiftFortuneWheel.swift b/Sources/SwiftFortuneWheel/SwiftFortuneWheel.swift index f965513..2a40864 100644 --- a/Sources/SwiftFortuneWheel/SwiftFortuneWheel.swift +++ b/Sources/SwiftFortuneWheel/SwiftFortuneWheel.swift @@ -415,7 +415,9 @@ public extension SwiftFortuneWheel { self.animator.addRotationAnimation(fullRotationsCount: 0, animationDuration: animationDuration, rotationOffset: rotation, - completionBlock: nil) + completionBlock: { _ in + self.pauseAllSound() + }) } @@ -429,7 +431,9 @@ public extension SwiftFortuneWheel { self.animator.addRotationAnimation(fullRotationsCount: 0, animationDuration: animationDuration, rotationOffset: rotationOffset, - completionBlock: nil) + completionBlock: { _ in + self.pauseAllSound() + }) } @@ -443,7 +447,13 @@ public extension SwiftFortuneWheel { DispatchQueue.main.async { self.stopRotation() - self.animator.addRotationAnimation(fullRotationsCount: fullRotationsCount, animationDuration: animationDuration, rotationOffset: rotationOffset, completionBlock: completion, onEdgeCollision: { [weak self] progress in self?.impactIfNeeded(for: .edge) + self.animator.addRotationAnimation(fullRotationsCount: fullRotationsCount, animationDuration: animationDuration, rotationOffset: rotationOffset, completionBlock: { finished in + if finished { + self.pauseAllSound() + } + completion?(finished) + }, onEdgeCollision: { [weak self] progress in + self?.impactIfNeeded(for: .edge) self?.onEdgeCollision?(progress) }) { [weak self] (progress) in @@ -523,6 +533,7 @@ public extension SwiftFortuneWheel { /// Stops rotation animation func stopRotation() { self.animator.stop() + self.pauseAllSound() } /// Starts rotation animation and stops rotation at the specified index and rotation angle offset diff --git a/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayable.swift b/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayable.swift index 6f7aa49..4414e0a 100644 --- a/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayable.swift +++ b/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayable.swift @@ -47,4 +47,9 @@ extension AudioPlayable { guard let _audioFile = audioFile else { return } playSound(audioFile: _audioFile) } + + /// Pause sound(s) + func pauseAllSound() { + audioPlayerManager.pauseAll() + } } diff --git a/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayerManager.swift b/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayerManager.swift index 51cbb8b..d938b59 100644 --- a/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayerManager.swift +++ b/Sources/SwiftFortuneWheel/Utils/Audio/AudioPlayerManager.swift @@ -40,12 +40,6 @@ class AudioPlayerManager { for _ in 0.. AudioPlayer { let player = AudioPlayer() engine.attach(player.node) - engine.connect(player.node, to: engine.mainMixerNode, format: nil) + // according to this: https://stackoverflow.com/a/55505717/3687801, we will defer connecting nodes to engine up until play command of player node + // connect is done in the performPlaybackOnFirstAvailablePlayer() function + // engine.connect(player.node, to: engine.mainMixerNode, format: nil) return player }