开发者

Could not start Audio Queue Error starting recording

开发者 https://www.devze.com 2023-01-29 04:51 出处:网络
CFStringRef state; UInt32 propertySize = sizeof(CFStringRef); //AudioSessionInitialize(NULL, NULL, NULL, NULL);
    CFStringRef state; 
    UInt32 propertySize = sizeof(CFStringRef); 
//  AudioSessionInitialize(NULL, NULL, NULL, NULL); 
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    if(CFStringGetLength(state) == 0)
//  if(state == 0)
    { //SILENT
        NSLog(@"Silent switch is on");
    //  create vibrate 
    //  AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    UInt32 audioCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty( kAudioSessionProperty_AudioCategory, sizeof(UInt32), &audioCategory);

    }
    else { //NOT SILENT
        NSLog(@"Silent switch is off");

    }

where ever i use Above code i am able to play sound file in Silent mode

but after playing recorded sound file in silent mode when i try to record voice again

I get an error

LIke

2010-12-08 13:29:56.710 VoiceRecorder[382:307] -66681 Could not start Audio Queue Error starting recording

here is the code

    // file url
[self setupAudioFormat:&recordState.dataFormat];
CFURLRef fileURL =  CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *) [filePath UTF8String], [filePath length], NO);
// recordState.currentPacket = 0;
    // new input queue
OSStatus status;
status = AudioQueueNewInput(&recordState.dataFormat, HandleInputBuffer, &recordState, CFRunLoopGetCurrent(),kCFRunLoopCommonModes, 0, &recordState.queue);
if (status) {CFRelease(fileURL); printf("Could not establish new queue\n"); return NO;}
    // create new audio file
status = AudioFileCreateWithURL(fileURL, kAudioFileAIFFType, &recordState.dataFormat, kAudioFileFlags_EraseFile, &recordState.audioFile);   C开发者_如何学PythonFRelease(fileURL); // thanks august joki
if (status) {printf("Could not create file to record audio\n"); return NO;}
    // figure out the buffer size
DeriveBufferSize(recordState.queue, recordState.dataFormat, 0.5, &recordState.bufferByteSize);      // allocate those buffers and enqueue them
for(int i = 0; i < NUM_BUFFERS; i++)
{
    status = AudioQueueAllocateBuffer(recordState.queue, recordState.bufferByteSize, &recordState.buffers[i]);
    if (status) {printf("Error allocating buffer %d\n", i); return NO;}

    status = AudioQueueEnqueueBuffer(recordState.queue, recordState.buffers[i], 0, NULL);
    if (status) {printf("Error enqueuing buffer %d\n", i); return NO;}
}       // enable metering
UInt32 enableMetering = YES;
status = AudioQueueSetProperty(recordState.queue, kAudioQueueProperty_EnableLevelMetering, &enableMetering,sizeof(enableMetering));
if (status) {printf("Could not enable metering\n"); return NO;}
    // start recording
status = AudioQueueStart(recordState.queue, NULL); //   status = 0;     NSLog(@"%d",status);
if (status) {printf("Could not start Audio Queue\n"); return NO;}
recordState.currentPacket = 0;
recordState.recording = YES;
return YES;

i get an error here


I was facing similar problem in iOS 7.1. Add following in AppDelegate's didFinishLaunchingWithOptions :

AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil]; 
[audioSession setActive:YES error: nil]; 

EDIT : Above code is working for me

0

精彩评论

暂无评论...
验证码 换一张
取 消