我正在开发一个以.wav格式录制音频的iPhone应用程序。
代码如下:
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应用程序录制。
发布于 2012-05-11 22:30:56
你不能用MP3录制,这是一种专有的编码格式。这些是我使用的设置,对于2分钟的记录时间,其结果是~150KB:
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。
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");发布于 2012-05-11 22:25:47
您不能在.mp3文件中录制音频。kAudioFormatMPEGLayer3仅适用于playback the .mp3 format audio。这就是no codec available for .mp3 recording。您需要记录为aac或wav格式,并将其转换为.mp3格式。
最好看看下面的链接和图片:

这里是一个学习音频格式转换基础知识的链接:
https://stackoverflow.com/questions/10549023
复制相似问题