Search code examples
objective-cwatchkitaudio-recordingsimulatorapple-watch

Is audio recording possible using Apple Watch Simulator?


I am trying to create audio recording functionality using Apple Watch Simulator(Watch OS 2 beta). But I am getting following error while calling presentAudioRecorderControllerWithOutputURL.

Error: Error Domain=com.apple.watchkit.errors Code=3 "(null)"

-(void)didSelectRowWithTag:(NSInteger)tag
 {
     NSString*strPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *strAudioFileName = [strPath stringByAppendingString:[NSString stringWithFormat:@"/%d.caf",tag]];
     NSURL *urlOutPut = [NSURL fileURLWithPath:strAudioFileName];
     NSDictionary *dictMaxAudioRec = @{@"WKAudioRecorderControllerOptionsMaximumDurationKey":@1800};

    [self presentAudioRecorderControllerWithOutputURL:urlOutPut preset:WKAudioRecorderPresetHighQualityAudio options:dictMaxAudioRec completion:^(BOOL didSave, NSError * error) {
        if(didSave)
        {
            NSLog(@"File Saved....");
        }

        NSLog(@"%@",error);
    }];
}

Solution

  • Watchkit error code 3 is Watchkit invalid argument error. It looks like the error could be in your output file path. You are appending it with name .caf which is not a supported type of audio file output. From the documentation it states for the URL parameter:

    The URL at which to store the recorded output. The filename extension determines the type of audio to record. You may specify the extensions .wav, .mp4, and .m4a.

    Here is example code that works to record audio.

    let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]  
    let url = NSURL(fileURLWithPath: path.stringByAppendingPathComponent("dictation.wav"))  
    self.presentAudioRecordingControllerWithOutputURL(url, preset: WKAudioRecordingPreset.NarrowBandSpeech, maximumDuration: 30, actionTitle: "Save") { (didSave, error) -> Void in  
    if let error = error {  
        print("error: \(error)")  
        return  
    }  
    
    if didSave {  
        print("saved!")  
        }  
    }  
    

    With this fix I am still not sure that the simulator will support audio recording. The simulator does not support every API call and you may need a real watch to test. Please update us with result.