4

我正在尝试在 GLES 应用程序(Unity3d)中编写麦克风功率计模块。它在 UIKit 应用程序中运行良好。但是当我集成到我的unity3d项目中时,AudioQueue无法启动属性。调用AudioQueueStart的结果代码总是-50,但是-50是什么意思呢?我在 iOS 开发人员库中找不到参考。

我已经搜索过这个问题,并且知道有人在 cocos2d 应用程序中遇到了同样的问题。也许这有一些相关性。

这是我的启动音频队列代码:

UInt32 ioDataSize = sizeof(sampleRate);
AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate, &ioDataSize, &sampleRate); //returns noErr
format.mSampleRate = sampleRate;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
format.mFramesPerPacket = format.mChannelsPerFrame = 1;
format.mBitsPerChannel = 16;
format.mBytesPerPacket = format.mBytesPerFrame = 2;

AudioQueueNewInput(&format, listeningCallback, self, NULL, NULL, 0, &queue); //returns noErr

AudioQueueBufferRef buffers[3];
for (NSInteger i = 0; i < 3; ++i) { 
    AudioQueueAllocateBuffer(queue, 735, &buffers[i]); //returns noErr
    AudioQueueEnqueueBuffer(queue, buffers[i], 0, NULL); //returns noErr
}

levels = (AudioQueueLevelMeterState *)calloc(sizeof(AudioQueueLevelMeterState), format.mChannelsPerFrame);
UInt32 trueValue = true;
AudioQueueSetProperty(queue, kAudioQueueProperty_EnableLevelMetering, &trueValue, sizeof(UInt32)); //returns noErr

AudioQueueStart(queue, NULL); //returns -50
4

2 回答 2

2

以下对我有用:

添加这个:

AVAudioSession * session = [AVAudioSession sharedInstance];

    if (!session) printf("ERROR INITIALIZING AUDIO SESSION! \n");
    else{


        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&nsError];

        if (nsError) printf("couldn't set audio category!");



        [session setActive:YES error:&nsError];
        if (nsError) printf("AudioSession setActive = YES failed");
    }

AudioQueueStart(myQueue, NULL);

于 2016-03-01T07:41:40.010 回答
0

您是否检查过 AudioSession 是否处于 AVAudioSessionCategoryRecord 或 AVAudioSessionCategoryPlayAndRecord 模式?

http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

于 2012-10-22T13:27:05.577 回答