I’ve a quite simple audio participant which performs mp3s from a url
func begin() {
if let url = URL(string: cloudinaryUrl) {
self.avPlayerItem = AVPlayerItem(url: url)
self.avPlayer = AVPlayer(playerItem: avPlayerItem)
self.avPlayer?.fee = Float(fee.fee)
self.avPlayer?.play()
}
}
And a slider that represents the observe:
///View Mannequin
func setTime(_ newValue: Double) {
guard let avPlayer = avPlayer, let avPlayerItem = avPlayerItem else { return }
let clampedValue = max(0, min(1, newValue))
let time = CMTimeGetSeconds(avPlayerItem.period) * clampedValue
let cmtime: CMTime = CMTimeMake(worth: Int64(time * 1000 as Float64), timescale: 1000)
avPlayer.search(to: cmtime, toleranceBefore: .zero, toleranceAfter: .zero)
}
// SwiftUI View
@State var slider: Double = 0
Slider(worth: $slider, in: 0...1, onEditingChanged: { transferring in
viewModel.setTime(slider)
})
This works properly, and I can transfer the slider to the place I would like the audio to start out and it does persistently.
Nevertheless, I’ve a listing of buttons with TimeIntervals e.g. 27.320. Once I click on the button I wish to bounce to the cut-off date with my audio participant. Nevertheless, it appears to not persistently go to the timestamp, and I am not sure why. Weirdly, it will play appropriately the primary time, then subsequent clicks play a second or two again.
// view mannequin
func setTranscriptTime(_ newValue: Double) {
guard let avPlayer = avPlayer else {
setError(error: .noPlayer)
return
}
let cmtime: CMTime = CMTimeMake(
worth: Int64(newValue * 1000 as Float64),
timescale: 1000
)
print(cmtime)
avPlayer.search(
to: cmtime,
toleranceBefore: .zero,
toleranceAfter: .zero
)
}
// view
Button(motion: {
viewModel.setTranscriptTime(dialogue[index].startTime)
}) {
Textual content(line.textual content)
}
This prints
CMTime(worth: 27320, timescale: 1000, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)
Which is what I anticipated and with the tolerance as zero, I am not sure why this could be off in any respect. Would recognize some recommendation! Thanks