无法启动音频队列错误开始录制

时间:2010-12-08 08:40:39

标签: iphone objective-c xcode ios4 silent

    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");

    }

我在哪里使用上面的代码我可以在静音模式下播放声音文件

但是当我尝试再次录制声音时,以静音模式播放录制的声音文件

我收到错误

LIke

2010-12-08 13:29:56.710 VoiceRecorder [382:307] -66681 无法启动音频队列 开始录制时出错

这是代码

    // 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);   CFRelease(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;

我在这里收到错误

1 个答案:

答案 0 :(得分:0)

我在iOS 7.1遇到了类似的问题。在AppDelegate's didFinishLaunchingWithOptions中添加以下内容:

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

编辑:上面的代码对我有用