iOS Swift Agora 无法录音

Agora cannot record call with iOS Swift

我正在关注这里的文档:https://docs.agora.io/en/Voice/rtc_recording_apple?platform=iOS 并实施基本记录。这是我的代码:

    func startRecording(){
        let filename = getDocumentsDirectory().appendingPathComponent("\(APP_NAME)\(now()).WAV")
        let str = String(describing: filename)
        self.recordingPath = str
        agoraKit?.startAudioRecording(str, quality: .high)
    }


    func stopRecording(){
     
        agoraKit?.stopAudioRecording()
        
        // get audio file
        guard let audioUrl = URL(string: self.recordingPath) as? URL else { return }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0 ) { [weak self] in

            // getdata
            do {
                let myData = try Data(contentsOf: audioUrl)
                print(myData.count, myData)
            } catch {
                print(error)
            }

        }        

    }
    
private func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
}

但我收到错误消息:The file “Whisper1608949569.WAV” couldn’t be opened because there is no such file

完整消息:

file:///var/mobile/Containers/Data/Application/1F682ABD-153C-4DFD-BFF4-

02C1CE6F9A4C/Documents/Whisper1608949569.WAV
Error Domain=NSCocoaErrorDomain Code=260 "The file “Whisper1608949569.WAV” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/1F682ABD-153C-4DFD-BFF4-02C1CE6F9A4C/Documents/Whisper1608949569.WAV, NSUnderlyingError=0x281e33f60 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

我没有正确访问文件吗?

这是我初始化 agora 客户端的方式:

    self.agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: AppID, delegate: self)
    agoraKit?.delegate = self
    agoraKit?.enableWebSdkInteroperability(true)

    // sample loudest speaker every second
    agoraKit?.enableAudioVolumeIndication(1000, smooth: 3, report_vad: true)

    agoraKit?.enableAudio()
    
    // config for livecast to start
    agoraKit?.setChannelProfile(.liveBroadcasting)
    
    // set framrate and HD/SD
    agoraKit?.setVideoEncoderConfiguration( CONFIG_PRODUCTION )
    
    //agoraKit?.setDefaultAudioRouteToSpeakerphone(true)

我刚刚查看了您正在使用的文档中引用的文档,它说 startAudioRecording(filepath, quality: quality) 方法现已弃用,您应该使用带有附加 sampleRate 参数的此方法:

https://docs.agora.io/en/Voice/API%20Reference/oc/Classes/AgoraRtcEngineKit.html#//api/name/startAudioRecording:sampleRate:quality:

同时检查startAudioRecording和stopAudioRecording的返回值returns 0,表示成功。

如果您的频道名称包含特殊字符(冒号、斜杠),录制将自动失败并且不会生成任何文件。 Agora 在创建临时文件时似乎使用了频道名称。