我们正在try 在我们的SwiftUI应用程序中实现AVPlayer seek,它在iOS 15.4之前就已经运行,但在更新之后就不再运行了.
let playerCurrentTime = CMTimeGetSeconds(player.currentTime())
let newTime = playerCurrentTime + 45
let time2: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
player.seek(to: time2, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero) { success in
print(success)
}
completionHandler立即被调用,success=false.
我们正在使用以下初始化代码从URL流式传输MP3文件:
playerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
player.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 2), queue: DispatchQueue.main) { _ in
if self.player.currentItem?.status == .readyToPlay {
self.currentTimeInSeconds = CMTimeGetSeconds(self.player.currentTime())
self.progressInPct = Double(self.currentTimeInSeconds) / Double(self.totalTimeInSeconds)
self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1
MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo
self.setupNowPlaying()
} else {
self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0
MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo
}
}
我们也试着在currentItem上搜索,但也没有成功.
player.currentItem?.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
还有谁经历过这样的事情,有什么建议吗?
更新:
struct testView: View {
var player = AVPlayer()
var body: some View {
Button {
self.startPlayer(url: episode.streamUrl!)
}
label: {
Text("Test")
}
}
func startPlayer(url: String) {
let playerItem = AVPlayerItem(url: URL(string: url) !)
self.player.replaceCurrentItem(with: playerItem)
player.play()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
let time2: CMTime = CMTimeMake(value: Int64(45 * 1000 as Float64), timescale: 1000)
player.seek(to: time2, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero) {
success in
print(success)
}
}
}
}
打印"假".