-1

我正在开发一个 iPhone 录音应用程序,它可以创建 caf 格式的音频文件。

当我播放这些文件时,我这里唯一的东西是静态的。此外,文件的大小非常大,例如 1.7 mb 记录 10 秒。

我可以用其他格式录制,例如 mp3。有没有人有一些如何做到这一点的示例代码?

谢谢

4

1 回答 1

0

你必须使用 AVAudioRecorder 类来做到这一点。这是开发人员参考的链接: AVAudioRecorder 参考

然后,您可以使用以下代码:

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

给你 !!

于 2010-03-30T13:20:14.500 回答