0
我有一個音頻應用程序,我正在播放前臺和後臺(鎖定屏幕)中的音頻文件列表。在播放背景音頻播放列表時遇到問題。Swift在後臺播放音頻播放列表
下面是我循環播放下一個播放列表後一個音頻文件的功能。我似乎每次都遇到了我的cellForItem問題。如果我不打開它,我會得到一個致命的錯誤。如果我把它放在一個if let語句中,那麼在播放下一首歌曲時,下一個重複代碼將不會貫穿它。我從不失去indexPath,所以它應該工作得很好。我無法弄清楚。
func playCellAudioInBackground(indexPath: IndexPath) {
if let item = cv.cellForItem(at: indexPath) as? CollectionViewCell {
let when = DispatchTime.now() + 3 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: ({
self.gradient.removeFromSuperlayer()
item.superview?.bringSubview(toFront: item)
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}), completion: { (finished: Bool) in
let audioURL = self.streams[indexPath.item][「audioURL"] as? String
let isLive = self.streams[indexPath.item]["isLive"] as? Bool
self.audioStreamSelected(audioUrl: audioURL!, live:isLive!) { (success) -> Void in
DispatchQueue.main.async {
self.audioPlay()
}
}
})
}
}
}