简体   繁体   English

iOS Swift 无法录音

[英]Agora cannot record call with iOS Swift

I am following the doc here:https://docs.agora.io/en/Voice/rtc_recording_apple?platform=iOS and implementing a basic recording.我在这里关注文档:https://docs.agora.io/en/Voice/rtc_recording_apple?platform=iOS并实现基本录音。 This is my code:这是我的代码:

    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]
}

But I am getting error: The file “Whisper1608949569.WAV” couldn't be opened because there is no such file但我收到错误消息: The file “Whisper1608949569.WAV” couldn't be opened because there is no such file

Full message:完整信息:

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"}}

Am I not accessing the file correctly?我没有正确访问文件吗?

This is how I have initialized the agora client:这就是我初始化 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)

I just checked out the documentation referenced from the doc you're using and it says the method startAudioRecording(filepath, quality: quality) is now deprecated, and you should instead use this method with the additional sampleRate parameter:我刚刚查看了您正在使用的文档中引用的文档,它说方法startAudioRecording(filepath, quality: quality) is now deprecated,您应该将此方法与附加的 sampleRate 参数一起使用:

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

Also check that the returned value of startAudioRecording and stopAudioRecording returns 0, meaning success.还要检查startAudioRecording和stopAudioRecording的返回值是否返回0,表示成功。

If your channel name contains special characters (colons, slashes) recording will silently fail and no file will be produced.如果您的频道名称包含特殊字符(冒号、斜杠),录制将无声地失败并且不会生成任何文件。 It seems Agora uses the channel name when creating the temporary file. Agora 在创建临时文件时似乎使用了频道名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM