我正在开发一种iPhone语音录制应用程序,它以caf格式创建音频文件。
当我回放这些文件时,我唯一的东西是静态的。而且,文件的大小很大,例如高达1.7 mb,10秒录音。
我可以用其他格式录制,例如mp3。有没有人有一些如何做的示例代码?
感谢
答案 0 :(得分:0)
您必须使用AVAudioRecorder类才能执行此操作。 以下是开发人员参考的链接: AVAudioRecorder reference
然后,您可以使用以下代码:
// Init audio with record capability
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Set recording setting
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEGLayer3] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:48000] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
// Create a AVAudioRecorder object
tmpUrl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"tmp.mp3"]]];
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpUrl settings:recordSetting error:nil];
// Start the recorder
[recorder record];
// record your voice here
[recorder stop];
// Play recorded sound
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:tmpUrl error:nil];
[player play];
你在这里!!