From a34e9661670c5281a77c66dfa28dd41f1ad6bbea Mon Sep 17 00:00:00 2001 From: Fabio Vinotti Date: Mon, 19 Jun 2023 00:38:23 +0200 Subject: [PATCH] Fix: seeking to playback duration always fails --- .../AudioPlayerNode/AudioPlayerNode.swift | 8 +++---- .../BasicAudioPlayer/BAPlayer/BAPlayer.swift | 21 ++++++++++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Sources/BasicAudioPlayer/AudioNodes/AudioPlayerNode/AudioPlayerNode.swift b/Sources/BasicAudioPlayer/AudioNodes/AudioPlayerNode/AudioPlayerNode.swift index 670aa63..0b5b043 100644 --- a/Sources/BasicAudioPlayer/AudioNodes/AudioPlayerNode/AudioPlayerNode.swift +++ b/Sources/BasicAudioPlayer/AudioNodes/AudioPlayerNode/AudioPlayerNode.swift @@ -57,9 +57,9 @@ public class AudioPlayerNode { set { let start = max(0, min(newValue.lowerBound, duration)) let end = max(0, min(newValue.upperBound, duration)) - - guard start < end else { - log.error("Failed to set playback region: duration <= 0.") + + guard start <= end else { + log.error("Failed to set playback region: duration < 0.") return } @@ -159,7 +159,7 @@ public class AudioPlayerNode { if let newSegment = segment { playbackSegment = newSegment } - + if segmentStart == duration { segmentStart = 0 } let startFrame = AVAudioFramePosition(segmentStart * file.fileFormat.sampleRate) diff --git a/Sources/BasicAudioPlayer/BAPlayer/BAPlayer.swift b/Sources/BasicAudioPlayer/BAPlayer/BAPlayer.swift index 38f85f8..6c59c62 100644 --- a/Sources/BasicAudioPlayer/BAPlayer/BAPlayer.swift +++ b/Sources/BasicAudioPlayer/BAPlayer/BAPlayer.swift @@ -15,7 +15,7 @@ import AVFoundation /// /// You can add an unlimited number of audio units to the player to affect the audio /// coming out of the player node. -public class BAPlayer: AudioPlayerNodeDelegate { +public class BAPlayer { // MARK: - Properties @@ -230,22 +230,27 @@ public class BAPlayer: AudioPlayerNodeDelegate { public func onStatusChange(perform action: @escaping (Status) -> Void) { onStatusChangeHandler = action } - - // MARK: - AudioPlayerNodeDelegate - + +} + +// MARK: - AudioPlayerNodeDelegate + +extension BAPlayer: AudioPlayerNodeDelegate { + public func playerNodeStatusDidChange(_ node: AudioPlayerNode, from oldStatus: AudioPlayerNode.Status, to newStatus: AudioPlayerNode.Status) { - + onStatusChangeHandler?(newStatus) } - + public func playerNodePlaybackDidComplete(_ node: AudioPlayerNode) { - playerNode.segmentStart = 0 + playerNode.segmentStart = duration playerNode.segmentEnd = playerNode.duration - + if !doesLoop { engine.stop() } } + }