如何在 AVQueuePlayer 的项目之间插入暂停?
How to insert a pause in between items in AVQueuePlayer?
我正在使用 AVQueuePlayer 播放一批音频文件。我想在项目之间插入一个暂停。知道怎么做吗?
这里是将项目添加到队列的方法:
func addItemToAudioQueue (file:String, last:Bool) {
let urlPath = Bundle.main.path(forResource: file, ofType: "mp3")
let fileURL = NSURL(fileURLWithPath:urlPath!)
let playerItem = AVPlayerItem(url:fileURL as URL)
if last {
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
}
queuePlayer.insert(playerItem, after: nil)
}
为名为 AVPlayerItemDidPlayToEndTimeNotification
的 notification
添加一个 observer
,每次播放完 AVPlayerItem
都会触发, 即
NotificationCenter.default.addObserver(self, selector: #selector(playerEndedPlaying), name: Notification.Name("AVPlayerItemDidPlayToEndTimeNotification"), object: nil)
AVPlayerItemDidPlayToEndTimeNotification
A notification that's posted
when the item has played to its end time.
接下来,当 playerEndedPlaying
方法在 notification
触发后被调用时,pause()
最初是 AVQueuePlayer
,然后 play()
在 5 之后再次调用像这样秒,
@objc func playerEndedPlaying(_ notification: Notification) {
self.player?.pause()
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {[weak self] in
self?.player?.play()
}
}
我正在使用 AVQueuePlayer 播放一批音频文件。我想在项目之间插入一个暂停。知道怎么做吗?
这里是将项目添加到队列的方法:
func addItemToAudioQueue (file:String, last:Bool) {
let urlPath = Bundle.main.path(forResource: file, ofType: "mp3")
let fileURL = NSURL(fileURLWithPath:urlPath!)
let playerItem = AVPlayerItem(url:fileURL as URL)
if last {
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
}
queuePlayer.insert(playerItem, after: nil)
}
为名为 AVPlayerItemDidPlayToEndTimeNotification
的 notification
添加一个 observer
,每次播放完 AVPlayerItem
都会触发, 即
NotificationCenter.default.addObserver(self, selector: #selector(playerEndedPlaying), name: Notification.Name("AVPlayerItemDidPlayToEndTimeNotification"), object: nil)
AVPlayerItemDidPlayToEndTimeNotification
A notification that's posted when the item has played to its end time.
接下来,当 playerEndedPlaying
方法在 notification
触发后被调用时,pause()
最初是 AVQueuePlayer
,然后 play()
在 5 之后再次调用像这样秒,
@objc func playerEndedPlaying(_ notification: Notification) {
self.player?.pause()
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {[weak self] in
self?.player?.play()
}
}