2

Is there any sample code or tutorials about that? I've found that AVAudioRecorder supported since WatchOS 4.0 https://developer.apple.com/documentation/avfoundation/avaudiorecorder. But when I am trying to use it - it records 1 second and no actual sound (just noise).

Here is my code:

let audioURL = self.getRecordedFileURL()
print(audioURL.absoluteString)

let settings = [
  AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
  AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]

do {
  recorder = try AVAudioRecorder(url: audioURL, settings: settings)
  recorder?.delegate = self
  recorder?.record()
} catch {
  finishRecording(success: false)
}

Also, should I use AudioSession here? If yes, is it required requestRecordPermission and how do deal with it? Thank you for your help!

1 Answer 1

4

This one works:

    let recordingName = "audio.m4a"
    let dirPath = getDirectory()
    let pathArray = [dirPath, recordingName]
    guard let filePath = URL(string: pathArray.joined(separator: "/")) else { return }

    let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                    AVSampleRateKey:12000,
                    AVNumberOfChannelsKey:1,
                    AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

    //start recording
    do {
        audioRecorder = try AVAudioRecorder(url: filePath, settings: settings)
        audioRecorder.delegate = self
        audioRecorder.record()
    } catch {
        print("Recording Failed")
    }
func getDirectory()-> String {
    let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    return dirPath
}

Don't forget to add NSMicrophoneUsageDescription into your phone companion app Info.plist.

3
  • Yep, the problem was that I don't add NSMicrophoneUsageDescription to phone companion app! Another quick question - can I request microphone usage directly from Apple Watch, not from iOS Application? Commented Jun 2, 2019 at 9:26
  • @VladyslaveSemenchenko the request will appear on Apple Watch, on the first launch, even when NSMicrophoneUsageDescription is added to companion phone app Info.plist. It's the only way to do it.<br> Don't forget to mark answer as correct one.
    – David
    Commented Jun 2, 2019 at 10:31
  • Thank you @david-kyslenko for your help! Commented Jun 2, 2019 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.