斯威夫特和录音

Adr*_*man 5 iphone audio-recording avaudiorecorder ios swift

我正在swift构建一个iOS应用程序,客户端返回了录音的问题,基本上它没有记录任何内容.他在带有ios 9.x的iPhone 5/5s上进行了测试.检查权限,如果手机上有足够的空间,这些都不是问题.

就个人而言,在模拟器上使用带有ios 10.x和iphone 5的iphone 6s设备测试并且记录正在运行.任何人之前遇到过这种情况,或者说我做错了什么让我望而却步.

为不同的功能添加了注释,说明应用程序的功能.

protocol RecordAndPlayManagerDelegate : class{
    func recorderDidStopRecording()
}

class RecordAndPlayManager: NSObject, AVAudioRecorderDelegate {
    var soundRecorder: AVAudioRecorder!
    let recordSettings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000,
        AVNumberOfChannelsKey: 1,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]
    class func directoryURL() -> URL? {
        let fileManager = FileManager.default
        let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = urls[0] as URL
        let soundURL = documentDirectory.appendingPathComponent("recording.m4a")
        return soundURL
    }

    //calling this from outside to get the audio
    class func getLocalOrRemoteRecording(_ recordingID : String!) -> URL?{

        let fileManager = FileManager.default
        let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = urls[0] as URL
        let soundURL = documentDirectory.appendingPathComponent(recordingID)

        if (fileManager.fileExists(atPath: soundURL.path)){
            return soundURL
        }

        let path = kBaseServerURLNOPORT + "data/" + recordingID
        let url = URL(string: path)

        return url

    }

    //called from outside, recordingID -> name of the file
    class func storeRecording(_ recordingID : String!) -> URL? {
        let fileManager = FileManager.default
        let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = urls[0] as URL
        let soundURL = documentDirectory.appendingPathComponent("recording.m4a")

        let data = try? Data(contentsOf: soundURL)
        //data here has only 28bytes and from this I know it didn't record. In my tests, I always get at least 20000 bytes.

        let newSoundURL = documentDirectory.appendingPathComponent(recordingID)
        try? data?.write(to: newSoundURL, options: [.atomic])

        do {
            try  FileManager.default.removeItem(at: soundURL)
        } catch _ {
            print("failed to delete file")
            return newSoundURL
        }

        return newSoundURL
    }

    //called on viewDidLoad in another screen
    func setupRecorder() {
        let url = DRMessageRecordAndPlayManager.directoryURL()!
        do {
            try self.soundRecorder = AVAudioRecorder(url: url, settings: self.recordSettings)
        } catch _ {
            return
        }
        soundRecorder.delegate = self
        soundRecorder.prepareToRecord()
    }

    //calling this from outside to start recording
    func startRecording() {
        if !self.soundRecorder.isRecording {
            let audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
                try audioSession.setActive(true)
                soundRecorder.record(forDuration: 15)
            } catch {
            }
        }
    }

    //called from outside when I hit stop
    func stopRecording() {

        self.soundRecorder.stop()
        let audioSession = AVAudioSession.sharedInstance()

        do {
            try audioSession.setActive(false)
        } catch {
        }
    }

    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
        self.delegate?.recorderDidStopRecording()
    }
}
Run Code Online (Sandbox Code Playgroud)

Cas*_*sey 5

我的猜测是catchin startRecording()正在按预期工作并防止崩溃,但你没有记录它.

我建议更改两个catch语句打印提供的错误对象,如下所示:

catch {
    // or use an alert controller to display the error
    print("caught error: \(error)")
}
Run Code Online (Sandbox Code Playgroud)