Skip to content

Commit

Permalink
Fix: seeking to playback duration always fails
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiovinotti committed Jun 18, 2023
1 parent 3416155 commit a34e966
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
21 changes: 13 additions & 8 deletions Sources/BasicAudioPlayer/BAPlayer/BAPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
}
}

}

0 comments on commit a34e966

Please sign in to comment.