HALOutput in AUGraph select and configure specific

2019-05-27 02:52发布

I successfully managed to build a complex AUGraph that I'm able to reconfigure on the fly, and all is working well.

I'm facing a wall now with what seems a very simple task: selecting a sepcific output device.

I'm able to get the deviceUID and ID thanks to this post: AudioObjectGetPropertyData to get a list of input devices (that I've modified to get output devices) and to the code below (I can't remember where I've found it, unfortunately)

- (AudioDeviceID) deviceIDWithUID:(NSString *)uid
{
AudioDeviceID myDevice;
AudioValueTranslation trans;

CFStringRef *myKnownUID = (__bridge CFStringRef *)uid;
trans.mInputData = &myKnownUID;

trans.mInputDataSize = sizeof (CFStringRef);
trans.mOutputData = &myDevice;
trans.mOutputDataSize = sizeof(AudioDeviceID);

UInt32 size = sizeof (AudioValueTranslation);
AudioHardwareGetProperty (kAudioHardwarePropertyDeviceForUID,
                          &size,
                          &trans);

return myDevice;

}

I'm getting the AudioDeviceID from this method which I store in an NSDictionary. I can NSLog it and when I convert it in hexadecimal it gives me the right ID, found in HALLab.

But when I configure my unit (see code below) the graph only plays on the default device (the one selected in Sound Preferences).

    AudioComponent comp = AudioComponentFindNext(NULL, &_componentDescription);

    if (comp == NULL) {
        printf ("Can't get output unit");
        exit (-1);
    }

    CheckError(AudioComponentInstanceNew(comp, &_auUnit),
               "Couldn't open component for output Unit");

    UInt32 disableFlag = 0;
    UInt32 enableFlag = 1;
    AudioUnitScope outputBus = 0;
    AudioUnitScope inputBus = 1;

    CheckError (AudioUnitSetProperty(_auUnit,
                                     kAudioOutputUnitProperty_EnableIO,
                                     kAudioUnitScope_Output,
                                     outputBus,
                                     &enableFlag,
                                     sizeof(enableFlag)), "AudioUnitSetProperty[kAudioOutputUnitProperty_EnableIO] failed - enable Output");

    CheckError (AudioUnitSetProperty(_auUnit,
                                     kAudioOutputUnitProperty_EnableIO,
                                     kAudioUnitScope_Input,
                                     inputBus,
                                     &disableFlag,
                                     sizeof(disableFlag)), "AudioUnitSetProperty[kAudioOutputUnitProperty_EnableIO] failed - disable Input");

    AudioDeviceID devID = (AudioDeviceID)[[[_devices objectAtIndex:0] objectForKey:@"deviceID"] unsignedIntValue];

    CheckError(AudioUnitSetProperty(_auUnit,
                                    kAudioOutputUnitProperty_CurrentDevice,
                                    kAudioUnitScope_Output,
                                    0,
                                    &devID,
                                    sizeof(AudioDeviceID)), "AudioUnitSetProperty[kAudioOutputUnitProperty_CurrentDevice] failed");

The AUGraph is already configured with all units, nodes are connected, and it's open. What am I doing wrong ?

I would be very grateful for any clue to resolve this problem.

0条回答
登录 后发表回答