Search code examples
objective-crecording

Watchkit presentAudioRecordingControllerWithOutputURL completion block issue


I'm simply trying to satisfy the arguments for the function below to bring up the audio recorder. I believe the issue is with the completion block. The error given in debug is :

presentAudioRecordingControllerWithOutputURL:preset:maximumDuration:actionTitle:completion: requires a non-NULL URL and completion block

NSBundle* myBundle = [NSBundle mainBundle];
NSURL* recording = [myBundle URLForResource:@"recording" withExtension:@"mp4"];

[self presentAudioRecordingControllerWithOutputURL:recording
                                            preset:WKAudioRecordingPresetWideBandSpeech
                                   maximumDuration:5000
                                       actionTitle:@"Recording"
                                        completion:^(BOOL didSave, NSError *error) {
                                            if (error != nil)
                                            {
                                               NSLog(@"Error: %@",error);
                                            }
                                            else if(didSave == YES)
                                            {
                                                NSLog(@"Saved the recording");
                                            }
                                        }];

Solution

  • I guess your file URL recording is wrong. You can't record into the "main bundle". So NSURL object couldn NOT be created and occurred the non-NULL error.

    For example, you would be able to record into the documents directory as follows:

    NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask,YES);
    NSString *path = [[filePaths firstObject] stringByAppendingPathComponent:@"recording.mp4"];
    NSURL *fileUrl = [NSURL fileURLWithPath:path];
    
    [self presentAudioRecordingControllerWithOutputURL:fileUrl
                                                preset:WKAudioRecordingPresetWideBandSpeech
                                       maximumDuration:5000
                                           actionTitle:@"Recording"
                                            completion:^(BOOL didSave, NSError * __nullable error) {
    
                                                NSLog(@"didSave:%d, error:%@", didSave, error);
                                            }];