iPhone 录音并导出/通过电子邮件发送

发布于 2024-09-17 06:21:47 字数 96 浏览 4 评论 0 原文

我搜索了苹果网站、互联网以及 Stackoverflow。有谁有任何示例代码或教程,说明如何从内置麦克风录制音频,然后通过电子邮件导出该音频?拜托,我的应用程序确实需要它。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-09-24 06:21:47

我使用 Apple 的 SpeakHere 示例应用程序中的代码将声音从内置麦克风录制到 wav 文件“recordedFile.wav”,然后将以下方法添加到视图控制器以通过电子邮件将 wav 文件作为附件发送

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

:从按钮获取 IBOutlet 方法。不要忘记在头文件中将控制器声明为 MFMailComposeViewControllerDelegate。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文