2013-07-01 102 views
3

我正在使用下面顯示的代碼保存wav文件。然後我將其添加到電子郵件中。但是,它不會通過電子郵件發送。記錄中似乎有錯誤。任何人都可以請幫忙。在文檔目錄中保存wav格式的記錄iphone

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]; 
+0

你檢查你的音頻文件目錄中的文件大小 – Purva

+1

是的,這是正確的4.3 MB。我認爲在代碼中有一些錯誤。在哪裏以及如何包含該文件是.wav?只是名字? – ScarletWitch

+0

你知道了嗎? – Purva

回答

2

如果以發送文件與iOS郵件的附件,你需要設置MIME類型的文件。根據this web site,用於WAV的MIME類型之一是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]; 

} 
+1

謝謝。是https://en.wikipedia.org/wiki/Internet_media_type中提到的mime type audio/wav或audio/vnd.wave? – ScarletWitch

1
- (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]; 

} 
0

使用MIME類型,你可以使用.wav文件按Tblue的答案。