【问题标题】:swift2 AVAudioRecorderswift2 AVAudioRecorder
【发布时间】:2023-03-10 03:05:01
【问题描述】:

我正在使用 swift 2 尝试以下代码,在 swift 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,

如果我注释掉该行,我通过了构建,但出现了运行时错误

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)

谁能帮帮我?

【问题讨论】:

    标签: ios swift2


    【解决方案1】:

    我也试过AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC)

    嗯,这是正确的说法。基本上你在这里问两个问题;您已经解决的编译器错误。现在您遇到了运行时错误,但这是完全不同的问题。

    至于运行时错误,它可能只是试图在模拟器上测试的虚构。我在设备上运行了你的代码(在修复了有问题的行以便它可以编译之后),这很好。

    编辑在评论中,您透露您在运行 iOS 8.3 的设备上对此进行了测试。那就是问题所在!你需要在设备上进行测试,并且需要是运行 iOS 9 的设备。然后你会发现你的代码运行没有崩溃。

    【讨论】:

    • 嗯...我打印出 audioURL 并显示 MyAudio.m4a 所以我认为它不为零。
    • 我测试了您的代码,它在设备上运行良好。您可能只是在模拟器上遇到问题,因为您无法在那里录制。
    • 不幸的是,它不适用于我的 iPhone 或模拟器。无论如何感谢您的测试!。
    • 我所做的唯一更改是将AVFormatIDKey: kAudioFormatMPEG4AAC 替换为AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC)。其余的完全如你所愿。它启动而不会崩溃。你需要我给你一份我的测试项目吗? — 另外,您还记得将您的设备更新到 iOS 9 吗?
    • 如果你能把你的测试项目发给我,那就太好了。我很感激。我没有将我的设备更新到 iOS9,但我将项目的构建更改为 iOS8.3。
    【解决方案2】:

    我在设置方面遇到了同样的问题。 AVFormatIDKey 不被接受,原帖中提到的错误。

    recordSettings 需要在 Swift 2 中显式转换。

    这些是我的录音设置。如果我只是跳过 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。我为 iOS 8.4 构建并运行了它

    【讨论】:

    • 非常很有帮助,谢谢。我已经追了好几天了。
    【解决方案3】:

    我遇到了类似的问题,结果当我从 swift 1.2 更新到 2.0 时,“设置”的签名更改为非可选的 [String : AnyObject]

    //Swift 1.2
    AVAudioRecorder(URL: audioURL!, settings: nil)
    

    如果我传递一个空字典,它就不会再崩溃了,并且像以前一样工作。

    //Swift 2.0    
    AVAudioRecorder(URL: audioURL!, settings: [:])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多