Question

I am saving a wav file using the code shown below. Then I am adding this to an email. However, it is not sent in email. It seems there is something wrong in the recording. Can anyone help please.

NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"MyAudioMemo.wav",
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];

// Define the recorder setting
recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt: 16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue:[NSNumber numberWithBool: NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue:[NSNumber numberWithBool: NO] forKey:AVLinearPCMIsFloatKey];
[recordSetting setValue:[NSNumber numberWithInt: AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

// Initiate and prepare the recorder
audioRecorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
audioRecorder.delegate = self;
audioRecorder.meteringEnabled = YES;
[audioRecorder prepareToRecord];
Was it helpful?

Solution

If order to send a file as an attachment with iOS Mail, you need to set the MIME type of the file. According to this web site, one of the MIME types used for WAV is audio/wav.

- (void)mailMe {

// recipient address
NSString *email = mailaddress;
NSMutableArray *toRecipient = [[NSMutableArray alloc]initWithObjects:nil];
[toRecipient addObject:email];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;

// attachment
NSData *data = [NSData dataWithContentsOfFile:[self filePathAudio]];
[mc addAttachmentData:data mimeType:@"audio/wav" fileName:@"MyAudioMemo.wav"];

// subject
[mc setSubject:mailsubject];

// message
[mc setMessageBody:msgbody isHTML:NO];

[mc setToRecipients:toRecipient];
[self presentViewController:mc animated:YES completion:NULL];

}

OTHER TIPS

- (void)mailPressed {

// recipient address

NSMutableArray *toRecipient = [[NSMutableArray alloc]initWithObjects:@"mail id here ",nil];

MFMailComposeViewController *mailcmp = [[MFMailComposeViewController alloc] init];
mailcmp.mailComposeDelegate = self;

// attachment
NSData *data = [NSData dataWithContentsOfFile:[self filePathAudio]];

[mailComposer addAttachmentData:myData mimeType:@"audio/wav" fileName:fileName] //file name is name of youw audio file

// set subject
[mailcmp setSubject:mailsubject];

// set message
[mailcmp setMessageBody:msgbody isHTML:NO];

[mailcmp setToRecipients:toRecipient];
[self presentViewController:mailcmp animated:YES completion:NULL];

}

Using MIME type you can use .wav file as per Tblue's Answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top