-->

iPhone应用程序:在MP3录音音频(iPhone App : recording audio i

2019-06-24 08:22发布

我开发一个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];

上面的代码记录以.wav格式的音频。 如果我想记录的,而不是kAudioFormatLinearPCM(.WAV)我需要什么其他的变化,使在用钥匙kAudioFormatMPEGLayer3(MP3)MP3音频? 变化recordSetting字典像的采样率,信道和所有

或推荐的任何音频格式在尺寸上与两个iPhone和Android兼容,更小,可以直接从iPhone应用程序记录

请帮助和建议。

谢谢。

Answer 1:

你不能在MP3记录,它是一种专有格式,当涉及到编码。 这些是我经常使用的设置,它出来到〜150KB用于记录时间2分钟:

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库做服务器上的音频转换的服务器。

编辑:这是Android记录的代码,它被设置为AMR编码,因为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");


Answer 2:

你不能记录.MP3文件中的音频。 kAudioFormatMPEGLayer3仅用于playback the .mp3 format audio 。 有no codec available for .mp3 recording 。 您需要纪录aacwav格式,并将其转换成.mp3格式。

更好地检查出以下链接和图像:

录音参考

这是学习基本的转换音频格式的链接:

音频转换器参考



文章来源: iPhone App : recording audio in mp3