iphone - 在iphone的Documents目录下保存一个wav格式的录音

标签 iphone objective-c avaudiorecorder

我正在使用下面显示的代码保存一个 wav 文件。然后我将其添加到电子邮件中。但是,它不会通过电子邮件发送。录音里好像有什么地方不对。任何人都可以帮忙。

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

最佳答案

如果要使用 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];

}

关于iphone - 在iphone的Documents目录下保存一个wav格式的录音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17398380/

相关文章:

iphone - xCode 3.2.1 外星人泄漏

iphone - 对具有范围对象的 NSMutablearray 进行排序

iphone - 如何重新启动我的 iPhone 应用程序

ios - 如何将元数据添加到 AVAudioRecorder 录制的 AAC 文件中?

ios - 访问麦克风后整个应用程序的音量变得更安静

iphone - 带有部分粗体文本的自定义 UITableViewCell

ios - 如何在 Storyboard上为 xcode 中的所有设备定位按钮

ios - xcode 将不同的最大字符长度设置为不同的 TextField

ios - 将 UIButton 旋转 180 度并旋转回来

ios - AVAudioRecorder 没有将录音保存到指定的路径