【发布时间】:2010-03-30 11:18:39
【问题描述】:
我正在开发一个 iPhone 录音应用程序,它可以创建 caf 格式的音频文件。
当我播放这些文件时,我这里唯一的东西是静态的。此外,文件的大小很大,例如高达 1.7 mb 的 10 秒录音。
我可以用其他格式录制,例如 mp3。有没有人有一些如何做到这一点的示例代码?
谢谢
【问题讨论】:
-
这个问题没有提供足够的细节来帮助你。我们需要更多信息,例如你具体是用什么方法录制的?
我正在开发一个 iPhone 录音应用程序,它可以创建 caf 格式的音频文件。
当我播放这些文件时,我这里唯一的东西是静态的。此外,文件的大小很大,例如高达 1.7 mb 的 10 秒录音。
我可以用其他格式录制,例如 mp3。有没有人有一些如何做到这一点的示例代码?
谢谢
【问题讨论】:
你必须使用 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];
你来了!!
【讨论】: