我正在使用AVPlayer从URL播放音频。一切都很正常,直到应用程序进入后台或iPhone进入睡眠模式。
如何防止发生这种情况。
我已经在info.plist中添加了以下内容:
以下是我的代码。
如何防止发生这种情况。
我已经在info.plist中添加了以下内容:
![enter image description here](https://istack.dev59.com/mhVGf.webp)
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let playTrackID = tracks[indexPath.row].trackID
let urlString = url
let url = NSURL(string: urlString)!
avPlayer = AVPlayer(URL:url)
avPlayer.play()
}
有什么想法吗?
更新:已被@Danil Gontovnik解决。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let playTrackID = tracks[indexPath.row].trackID
let urlString = url
let url = NSURL(string: urlString)!
do {
avPlayer = AVPlayer(URL:url)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
}
catch {
print("Something bad happened. Try catching specific errors to narrow things down")
}
avPlayer.play()