首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iPhone应用程序中录制mp3格式的音频

问在iPhone应用程序中录制mp3格式的音频
EN

Stack Overflow用户
提问于 2012-05-11 17:43:56
回答 2查看 19.4K关注 0票数 7

我正在开发一个以.wav格式录制音频的iPhone应用程序。

代码如下:

代码语言:javascript
复制
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc]init];         
[recordSetting setValue :[NSNumber  numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey];         
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];




NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
 NSUserDomainMask, YES);

  NSString *documentsDirectory = [path objectAtIndex:0]; 

  NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:@"test.wav"];

 recordedTmpFile = [NSURL fileURLWithPath:myDBnew];

NSLog(@"Using File called: %@",recordedTmpFile);

        recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];

上面的代码是录制.wav格式的音频。如果我想使用key kAudioFormatMPEGLayer3(mp3)而不是kAudioFormatLinearPCM(.wav)在MP3中录制音频,我还需要做哪些更改?recordSetting字典中的更改,如采样率、通道和所有。

或者推荐任何兼容iPhone和安卓的音频格式,尺寸较小,可以直接从iPhone应用程序录制。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-11 22:30:56

你不能用MP3录制,这是一种专有的编码格式。这些是我使用的设置,对于2分钟的记录时间,其结果是~150KB:

代码语言:javascript
复制
NSString *tempDir = NSTemporaryDirectory();
NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                                  [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                  [NSNumber numberWithInt:16], AVEncoderBitRateKey,
                                  [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                  [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                  [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
                                  nil];

执行转换也是一个处理器密集型操作,如果您要将此音频发送到服务器,您可以使用带mp3lame库的FFMPEG在服务器上执行音频转换。

编辑:这是用于安卓录制的代码,它被设置为AMR编码,因为只有Honeycomb支持AAC。

代码语言:javascript
复制
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioChannels(1);
mediaRecorder.setOutputFile("sample.m4a");
票数 25
EN

Stack Overflow用户

发布于 2012-05-11 22:25:47

您不能在.mp3文件中录制音频。kAudioFormatMPEGLayer3仅适用于playback the .mp3 format audio。这就是no codec available for .mp3 recording。您需要记录为aac或wav格式,并将其转换为.mp3格式。

最好看看下面的链接和图片:

Audio Recording Reference

​

​

这里是一个学习音频格式转换基础知识的链接:

Audio Converter Reference

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10549023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档