我正在尝试下面的代码使用斯威夫特2,这应该是罚款的斯威夫特1。
class NewSoundViewController: UIViewController {
required init(coder aDecoder: NSCoder) {
let audioURL = NSURL.fileURLWithPathComponents([
NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0],
"MyAudio.m4a"
])
do {
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch {
print("Session errors.")
}
do {
let recordSettings: [String: AnyObject] = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 2,
]
self.audioRecorder = try AVAudioRecorder(URL: audioURL!, settings: recordSettings)
self.audioRecorder.meteringEnabled = true
self.audioRecorder.prepareToRecord()
} catch let error as NSError{
print(error.description)
} catch {
print("Other errors")
}
super.init(coder: aDecoder)
}我有编译错误
类型'AudioFormatID‘不符合’AnyObject‘’‘协议
在AVFormatIDKey: kAudioFormatMPEG4AAC,线上。
如果我注释掉这一行,我通过了build,但是得到了一个运行时错误。
Error Domain=NSOSStatusErrorDomain Code=1718449215“操作无法完成。(OSStatus错误1718449215.)"
我还尝试了AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),,得到了运行时错误。Xcode似乎进入调试模式,它红色突出显示self.audioRecorder = try AVAudioRecorder(URL: audioURL!, settings: recordSettings),并说
线程1: EXC_BAD_ACCESS(code=1,address=0x0)
有人能帮帮我吗?
发布于 2015-06-28 01:51:24
我也试过
AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC)
好吧,这是正确的说法。基本上,您在这里提出了两个问题:您已经解决的编译器错误。现在您有一个运行时错误,但这是完全不同的事情。
至于运行时错误,这可能只是试图在模拟器上进行测试的假象。我在设备上运行了您的代码(在修复了所讨论的行以便进行编译之后),它很好。
在编辑的评论中,您发现您是在运行iOS 8.3的设备上测试的。这就是问题所在!你需要在一个设备上进行测试,它需要是一个运行iOS 9的设备,然后你会发现你的代码不会崩溃。
发布于 2015-09-20 18:38:05
我在设置上也有同样的问题。在最初的文章中提到的错误中,AVFormatIDKey没有被接受。
需要在Swift 2中显式地抛出recordSettings。
这是我的录音设置,可以工作。如果我跳过AVFormatIDKey,麦克风只工作一小会儿。
let recordSettings = [AVSampleRateKey : NSNumber(float: Float(44100.0)),
AVFormatIDKey : NSNumber(int: Int32(kAudioFormatAppleLossless)),
AVNumberOfChannelsKey : NSNumber(int: 1),
AVEncoderAudioQualityKey : NSNumber(int: Int32(AVAudioQuality.Medium.rawValue)),
AVEncoderBitRateKey : NSNumber(int: Int32(320000))]您不需要将设备升级到iOS 9。
发布于 2015-09-07 07:54:45
我也遇到了类似的问题,结果是当我将“设置”的签名从Web1.2更新到2.0时,“设置”的签名变成了一个非可选字符串: AnyObject
//Swift 1.2
AVAudioRecorder(URL: audioURL!, settings: nil)如果我把一本空字典递给我,它就不会再为我崩溃了,而且能像以前一样工作。
//Swift 2.0
AVAudioRecorder(URL: audioURL!, settings: [:])https://stackoverflow.com/questions/31095234
复制相似问题