为什么使用kAudioFormatMPEGLayer3格式时AVAudioRecorder引发错误?

时间:2018-07-31 13:45:45

标签: ios swift avaudiorecorder

我正在尝试以mp3格式录制音频,但是当我使用kAudioFormatMPEGLayer3 AVAudioRecorder时会抛出错误。

代码是

获取文件路径

func getFileUrl() -> URL
{
    let filename = "myRecording.mp3"
    let filePath = getDocumentsDirectory().appendingPathComponent(filename)
    return filePath
}

创建AVAudioRecorder

  func setup_recorder()
{
       if isAudioRecordingGranted
       {
        let session = AVAudioSession.sharedInstance()
        do
        {
            try  session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
            try session.setActive(true)
            let settings = [
                AVFormatIDKey: Int(kAudioFormatMPEGLayer3),
                AVSampleRateKey: 44100,
                AVNumberOfChannelsKey: 2,
                AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue
            ]
            audioRecorder = try AVAudioRecorder(url: getFileUrl(), settings: settings)
            audioRecorder.delegate = self
            audioRecorder.isMeteringEnabled = true
            audioRecorder.prepareToRecord()
        }
        catch let error {
            display_alert(msg_title: "Error", msg_desc: error.localizedDescription, action_title: "OK")
        }
    }
    else
    {
        display_alert(msg_title: "Error", msg_desc: "Don't have access to use your microphone.", action_title: "OK")
    }
}

谢谢。

0 个答案:

没有答案