views:

23

answers:

1

I have searched all over Apples website and the internet as well as Stackoverflow. Does anyone have any sample code or a tutorial of how I can record audio from the built in mic, and then export that audio via email? Please, I really need it for my app. Thanks.

A: 

I used code from Apple's SpeakHere example app to record sound from the built-in mic to the wav file "recordedFile.wav", then added the following methods to the view controller to email the wav file as an attachment:

- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error {
        [self becomeFirstResponder];
        [self dismissModalViewControllerAnimated:YES];
}

- (void)mailAttachedWavFile {
        MFMailComposeViewController *picker = 
          [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
        [picker setSubject:@"My Wav File"];  // optional
        NSString *fileName = @"recordedFile.wav";  // whatever
        NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
        NSData   *data = [NSData dataWithContentsOfFile:path];
        [picker addAttachmentData:data mimeType:@"audio/x-wav"
                                       fileName:fileName];
        NSString *emailBody = @"Wav format sound file attached.";  // optional
        [picker setMessageBody:emailBody isHTML:YES];
        [self presentModalViewController:picker animated:YES];
        [picker release];
}

You can make the mailAttachedWavFile method an IBOutlet from a button. Don't forget to declare the controller as a MFMailComposeViewControllerDelegate in the header file.

hotpaw2
Would you mind sharing sample xcode project please? I tried this but failed. You could email it to me
Henry D'Andrea
What failed? Did you get the SpeakHere example to save a wav audio file? Did the above code fail to compile?
hotpaw2
I am not sure how I would declare it nor create an IBAction for it. Im a little new to this. A Sample project would be better. henryfbemail[at]gmail.com
Henry D'Andrea