iPhone App:用mp3录制音频 [英] iPhone App : recording audio in mp3

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

问题描述

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

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

同样进行转换是处理器密集型操作,如果你要将此音频发送到服务器,你可以使用带有mp3lame库的FFMPEG在服务器上进行音频转换。

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录音的代码,它设置为AMR编码,因为只有Honeycomb支持AAC。

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 App:用mp3录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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