如何在 Xcode 中正确创建可在 iOS 邮件中听到的音频文件

发布于 2025-01-08 04:15:14 字数 1596 浏览 0 评论 0原文

我有一个具有录制和播放功能的应用程序。这些工作正常。我还可以通过电子邮件发送音频文件。

如果我在计算机上阅读电子邮件,我可以播放音频文件。

我可以在 iDevice 上阅读电子邮件,但如果我尝试播放音频文件,我会看到类似 QT 的内容弹出一会儿,但随后该消息又返回到屏幕上。

如果我按住音频文件图标,我会得到可以使用的应用程序列表 - 但它们都是文档应用程序,而不是音频应用程序。

声音文件具有 .caf 后缀(核心音频格式)。

我的音频文件是这样创建的:

NSMutableDictionary* recordSettings = [[NSMutableDictionary alloc] init];

[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4]     forKey:AVFormatIDKey];
[recordSettings setValue :[NSNumber numberWithInt:16]                           forKey:AVEncoderBitRateKey];
[recordSettings setValue :[NSNumber numberWithFloat:44100.0]                    forKey:AVSampleRateKey]; 
[recordSettings setValue :[NSNumber numberWithInt: 2]                              forKey:AVNumberOfChannelsKey];

NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]

initWithURL:soundFileURL settings:recordSettings error:&error];

if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
// ALL IS OK, START RECORDING
//NSLog(@"DetailVC - recordAudio -  soundFilePath is %@", soundFile);
[audioRecorder prepareToRecord];
recordToggle = 1;

[autoCog startAnimating];
[audioRecorder record];     

recordingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                         target:self
                                                       selector:@selector(recordingOn)
                                                       userInfo:nil
                                                        repeats:YES];

}

感谢您的任何建议。

I have an app that has record and playback capabilities. These work fine. I can also email the audio file.

If I read the email on a computer, I can play the audio file.

I can read the email on an iDevice, but if I try to play the audio file, I see something like QT pop up for a moment, but then, the message returns to the screen.

If I hold down the audiofile icon, I get a list of apps that can be used - but they are all document apps, not audio apps.

The soundfile has a .caf suffix (Core Audio Format).

My audio file is created like this:

NSMutableDictionary* recordSettings = [[NSMutableDictionary alloc] init];

[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4]     forKey:AVFormatIDKey];
[recordSettings setValue :[NSNumber numberWithInt:16]                           forKey:AVEncoderBitRateKey];
[recordSettings setValue :[NSNumber numberWithFloat:44100.0]                    forKey:AVSampleRateKey]; 
[recordSettings setValue :[NSNumber numberWithInt: 2]                              forKey:AVNumberOfChannelsKey];

NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]

initWithURL:soundFileURL settings:recordSettings error:&error];

if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
// ALL IS OK, START RECORDING
//NSLog(@"DetailVC - recordAudio -  soundFilePath is %@", soundFile);
[audioRecorder prepareToRecord];
recordToggle = 1;

[autoCog startAnimating];
[audioRecorder record];     

recordingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                         target:self
                                                       selector:@selector(recordingOn)
                                                       userInfo:nil
                                                        repeats:YES];

}

Thanks for any advice.

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

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

发布评论

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

评论(2

风筝有风,海豚有海 2025-01-15 04:15:14

问题可能不在于音频创建,而在于如何将其附加到电子邮件中。您是否正确设置 MIME 类型以便邮件客户端知道如何读取数据?对于 .caf,您应该使用 audio/x-caf MIME 类型。

The problem might not be in the audio creation, but how you attach it to the email. Are you setting the MIME type correctly so the mail client knows how to read the data? For .caf, you should use the audio/x-caf MIME type.

温暖的光 2025-01-15 04:15:14

解决该问题的代码是:

NSData *soundData = [NSData dataWithContentsOfFile:soundFile];
[mailer addAttachmentData:soundData mimeType:@"audio/mpeg" fileName:@"YourFile.mp3"]; 

The code to solve the problem was:

NSData *soundData = [NSData dataWithContentsOfFile:soundFile];
[mailer addAttachmentData:soundData mimeType:@"audio/mpeg" fileName:@"YourFile.mp3"]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文