ios*_*ios 7 iphone objective-c avaudioplayer avaudiorecorder ios
我正在开发一个iPhone应用程序,它以.wav格式记录音频.
这是代码
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];
Run Code Online (Sandbox Code Playgroud)
上面的代码是以.wav格式录制音频.如果我想使用密钥kAudioFormatMPEGLayer3(mp3)而不是kAudioFormatLinearPCM(.wav)在MP3中录制音频,我需要做出哪些其他更改?recordSetting字典中的更改,如采样率,频道和所有
或建议任何兼容iPhone和Android的音频格式,尺寸较小,可直接从iPhone App录制
请帮助和建议.
谢谢.
8vi*_*ius 25
你无法用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];
Run Code Online (Sandbox Code Playgroud)
进行转换也是处理器密集型操作,如果您将此音频发送到服务器,则可以使用带有mp3lame库的FFMPEG在服务器上进行音频转换.
编辑:这是Android录制的代码,它设置为AMR编码,因为AAC仅支持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");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16988 次 |
| 最近记录: |