在iPhone应用程序中以mp3格式录制音频 [英] Recording audio in mp3 format in an iPhone app

查看:171
本文介绍了在iPhone应用程序中以mp3格式录制音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iPhone应用程序,该应用程序以.wav格式记录音频.

I am developing an iPhone app which records the audio in .wav format.

这是代码:

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格式录制音频.如果我想使用键kAudioFormatMPEGLayer3(mp3)而不是kAudioFormatLinearPCM(.wav)在MP3中录制音频,我还需要进行哪些其他更改? recordSetting词典中的更改,例如采样率,频道等.

Above code is recording audio in .wav format. If I want to record audio in MP3 using key kAudioFormatMPEGLayer3(mp3) instead of kAudioFormatLinearPCM(.wav), what other changes do I need to make? Changes in recordSetting dictionary like sample rate, channel and all.

或建议使用与iPhone和Android兼容的任何音频格式,其尺寸较小并且可以直接从iPhone应用程序记录.

Or suggest any audio format which is compatible with both iPhone and Android, smaller in size and can be recorded directly from iPhone app.

推荐答案

您无法以MP3格式录制,这是编码时的专有格式.这些是我使用的设置,在2分钟的录制时间内显示为〜150KB:

You cannot record in MP3, it is a proprietary format when it comes to encoding. These are the settings I use, it comes out to ~150KB for 2 min of recording time:

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

进行转换也是处理器的一项繁重操作,如果要将音频发送到服务器,则可以将FFMPEG与mp3lame库一起使用来在服务器上进行音频转换.

Also doing conversion is a processor intense operation, if you're sending this audio to a server you can use FFMPEG with the mp3lame library to do audio conversion on the server.

编辑:这是Android录制的代码,由于仅从Honeycomb开始才支持AAC,因此将其设置为AMR编码.

this is the code for Android recording, it's set to AMR encoding because AAC is only supported as of Honeycomb.

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");

这篇关于在iPhone应用程序中以mp3格式录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆