AVAudioRecorder prepareToRecord works, but record fails

别说谁变了你拦得住时间么 提交于 2019-12-25 00:52:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!