Search code examples
macoscore-audioairplay

Streaming audio to multiple AirPlay devices


Anyone know how to stream audio to multiple AirPlay destinations? Apparently, this was possible through Core Audio at some point in the past, but on 10.9 and 10.10, this does not seem possible. iTunes does it, so what's the secret? Here is some code I tried to see if I could get this to work:

OSStatus err = 0;
UInt32 size = sizeof(UInt32);

SSAudioSource * targetSource =  airplayDevice.airplaySources[0];
AudioDeviceID airPlayDeviceID = targetSource.deviceID;
    SSAudioSource * source1 = airplayDevice.airplaySources[0];
    SSAudioSource * source2 = airplayDevice.airplaySources[1];
    SSAudioSource * source3 = airplayDevice.airplaySources[2];

    AudioDeviceID alldevices[] = {source3.sourceID, source2.sourceID, source1.sourceID};
AudioObjectPropertyAddress addr;
addr.mSelector = kAudioDevicePropertyDataSource;
addr.mScope = kAudioDevicePropertyScopeOutput;
addr.mElement = kAudioObjectPropertyElementMaster;

// Set the 'AirPlay' device to point to all of its sources...
err = AudioObjectSetPropertyData(airPlayDeviceID, &addr, 0, nil, size, alldevices);

AudioObjectPropertyAddress audioDevicesAddress = {
    kAudioHardwarePropertyDefaultOutputDevice,
    kAudioObjectPropertyScopeGlobal,
    kAudioObjectPropertyElementMaster
};

// ...now set the system output to point at the 'AirPlay' device
err = AudioObjectSetPropertyData(kAudioObjectSystemObject, &audioDevicesAddress, 0, nil, size, &airPlayDeviceID);

No matter how I arrange the devices in the array, sound only comes out of the first device (index 0) of the array. So what's the secret? Thanks


Solution

  • I raised a bug report with Apple for this back in July and got a reply in October:

    Engineering has determined that there are no plans to address this issue.

    I've gone back to Apple asking why the functionality has been removed but not hopeful for a (timely) response.

    For what it's worth I think your approach is correct, it's similar to the way I had it working in the past for an app. I suspect iTunes uses Audio Units or something similar to do multiple speakers.