gpt4 book ai didi

ios - AVAudioSession.sharedInstance 没有被调用

转载 作者:行者123 更新时间:2023-11-28 08:13:57 25 4
gpt4 key购买 nike

我正在创建一个音频应用程序,我希望音频在屏幕关闭时继续播放。(它在屏幕打开时在后台运行良好。)

在 xcode 上启用了后台模式(我在 Info.plist 中设置了“音频”值。)并且我实现了“AVAudioSession.sharedInstance”,但是当我的设备屏幕关闭时应用程序崩溃了。有解决这个问题的想法吗?

完整代码如下

var player:AVAudioPlayer!

init(withStartingClosure block1:@escaping(_ dict:[String:Any],_ index:Int) -> Void,andStopingClosure block2:@escaping(_ index:Int) -> Void,finishedAllClosure block3:((_ finished:Bool) -> Void)? = nil) {
self.didStartPlaying = block1
self.didStopPlaying = block2
self.didFinishPlaying = block3
}
deinit {
self.mode = .stopped
}
}
extension AudioPlayer {
func getURL(ofAudio name:String) -> URL {
let url = URL(string: name)!
let nm = url.deletingPathExtension().absoluteString
let type = url.pathExtension
let path = Bundle.main.url(forResource: nm, withExtension: type)!
return path
}

func setupPlayer(withData data:[String:Any]){
let name = data["audio"] as! String
var idx = self.currentIndex - 1
if idx < -1 {
idx = -1
}
print("\nCurrentIndex = \(idx),and Audio is = \(data)")
let path = self.getURL(ofAudio: name)
do {
try self.setupAudioSession()
self.player = try AVAudioPlayer(contentsOf: path)
self.player.delegate = self
if self.player.prepareToPlay() == true {
self.player.play()
self.didStartPlaying?(data, idx)
}else{
print("----------Can't Play \(data)")
}
}
catch {
print(error.localizedDescription)
let alert = UIAlertView(title: "Error!", message: error.localizedDescription, delegate: nil, cancelButtonTitle: "OK")
alert.show()
}

let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
} catch {
fatalError("error1")
}

do {
try session.setActive(true)
} catch {

fatalError("sessionError")
}

}

我收到的错误。由于未捕获的异常“NSRangeException”而终止应用程序,原因:“-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]

func reloadCell(AtIndexPath indexPath:IndexPath,shouldStop value:Int = 0){
//"value" is default value.
// if it is 1. it means stop playing animation of this TableViewCell
let cell = self.table.cellForRow(at: indexPath)
self.configureCell(cell, atIndexPath: indexPath, shouldStop: value)

//This is the code which get crash
self.table.scrollToRow(at: indexPath, at: .bottom, animated: true)

}

最佳答案

NSRangeException。这意味着您正在调用一个单元格 - TableView 中不存在的索引路径。

关于ios - AVAudioSession.sharedInstance 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com