gpt4 book ai didi

audio - AVAudioPlayer 不工作。 swift2 中的错误域=NSOSStatusErrorDomain 代码=1954115647 "(null)"

转载 作者:行者123 更新时间:2023-12-02 22:49:11 27 4
gpt4 key购买 nike

我正在从服务器下载插图、标题和音频数据文件,并能够在表格单元格中显示。

单击表格单元格中的播放按钮时,将音频数据保存到本地文件中,然后尝试从文档文件路径播放。音频未在 AVAudioPlayer 上播放并出现错误。

搜索了很多,但到目前为止还没有找到解决方案。您能否更正我的代码并帮助解决此问题。下面是我的代码。

class AudioViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, AVAudioPlayerDelegate {

var audioPlayer: AVAudioPlayer?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let myCell:AudioTableViewCell = tableView.dequeueReusableCellWithIdentifier("audioCell", forIndexPath: indexPath) as! AudioTableViewCell

myCell.lblAudioTitle.text = arrDictAudios[indexPath.row]["title"] as? String

let artworkData = arrDictAudios[indexPath.row]["artwork"] as? NSData
myCell.imgArtwork.image = UIImage(data: artworkData!)

myCell.btnOutletPlay.tag = indexPath.row
myCell.btnOutletPlay.addTarget(self, action: "playButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

return myCell
}

func playButtonClicked(sender: AnyObject) {

let buttonRow = sender.tag
let buttonRowNSNumber = buttonRow as NSNumber

let audioData = arrDictAudios[buttonRow]["data"] as? NSData
print(audioData?.length)


let audioFileNameWithExt = NSString(format: "/%@.m4a",buttonRowNSNumber.stringValue)
var documentsDirectory:String?

var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

if paths.count > 0 {

documentsDirectory = paths[0] as? String

let savePath = documentsDirectory! + (audioFileNameWithExt as String)

NSFileManager.defaultManager().createFileAtPath(savePath, contents: audioData, attributes: nil)

print("savedPath %@", savePath)
/var/mobile/Containers/Data/Application/8A6AEAAE-B144-4EB2-A70C-99520A0AD9D3/Documents/1.m4a


do {
audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: savePath), fileTypeHint: AVFileTypeAppleM4A)
audioPlayer!.prepareToPlay()
audioPlayer!.play()
}
catch let error as NSError{
print(error.description)
//Error Domain=NSOSStatusErrorDomain Code=1954115647 "(null)"
}

}

}
}

最佳答案

请尝试此代码。

func play() {

if let data = NSData(contentsOfURL: savePath) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .AllowBluetooth)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(data: data, fileTypeHint: AVFileTypeAppleM4A)
audioPlayer!.prepareToPlay()
audioPlayer!.play()

} catch let error as NSError {
print("Unresolved error \(error.debugDescription)")
}
}
}

关于audio - AVAudioPlayer 不工作。 swift2 中的错误域=NSOSStatusErrorDomain 代码=1954115647 "(null)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35593059/

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