iPhone: Recording to CAF Files Produces Only Static
I am developing an iPhone voice recording application which creates audio files in caf format.
When I play those files back, the only thing I here is static. Also, the sizes of the files are huge e.g. up to 1.7 mb for a 10 sec recording.
Could I record in another format such as mp3. Does anyone have some example c开发者_如何学Code of how to do so?
thanks
You have to use AVAudioRecorder class to do that. Here is a link to developer reference: AVAudioRecorder reference
Then, you can use the following code:
// 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];
Here you are !!
精彩评论