问题
I just built and tested a basic AVAudioRecorder/AVAudioPlayer sound recorder and player. Eventually I got this working on the device, as well as the simulator. My player/recorder code is in a single UIView subclass. Unfortunately, when I copy the class into my main project, it no longer works (on the device--the simulator is fine). prepareToRecord
is working fine, but record
isn't. Here is some code:
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
if ([audioRecorder prepareToRecord]){
audioRecorder.meteringEnabled = YES;
if(![audioRecorder record])NSLog(@"recording failed!");
}else {
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@"preparedToRecord=NO Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);
...
I get "recording failed". Anyone have any ideas why this is happening?
回答1:
The complete solution was to move the [SimpleAudioEngine sharedEngine]
initialisation to before my own AVAudioSession setCategory
. I think somewhere in SimpleAudioEngine
, its doing a setCategory :AVAudioSessionCategoryAmbient
. Overriding this with my own setCategory :AVAudioSessionCategoryPlayAndRecord
later makes everything work.
来源:https://stackoverflow.com/questions/4861871/avaudiorecorder-preparetorecord-works-but-record-fails