How can I programmatically create a multi-output device in OS X?

佐手、 提交于 2019-12-07 08:00:26

问题


How can I programmatically create a Multi-Output Device in Mac OS X?

The Audio Midi Setup program provides a GUI interface for creating one, but I would like to be able to create one in code.

I've found some resources already for creating aggregate devices, but multi-output devices function differently and I can't find anything on creating them. Here's what I've got so far:

  • How to combine multiple audio interfaces by creating an aggregate device
  • Using Aggregate Devices
  • Creating Core Audio aggregate devices programmatically
  • What is the difference between an “aggregate” and a “multi-output” device?

回答1:


I had the same need to create a Multi Output Device programmatically. This example: http://daveaddey.com/?p=51 covers the needs.

I managed to do it by studying /Library/Preferences/Audio/com.apple.audio.SystemSettings.plist. This is the place where the Audio MIDI Setup saves the configuration (on my box, El Capitan).

Create a Multi-Output Device and an Aggregate Device and study the plist. They're pretty much the same, except the fact that the Aggregate Device has a master device, while the MOD does not.

Considering the example from Dave Addey: I for one, managed to create the MOD by removing the master device and adding my own devices as outputs. (in my case, Soundflower and the Default Output)

Besides this, add the "stacked" boolean key to the CF Dictionary. According to the CoreAudio sources, this key ensures that the same output is sent to all output channels, as opposed to the Aggregate Device which is not stacked, and combines multiple devices to make itself 'look' as a single device with a lot of channels.

Hope this helps.




回答2:


This is basically @Andrei B.'s answer, with the linked [now dead] blog post code updated to use AudioHardwareCreateAggregateDevice() and the kAudioAggregateDeviceIsStackedKey symbol (and it's in swift, sorry, I didn't read the question):

func createMultiOutputAudioDevice(masterDeviceUID: CFString, secondDeviceUID: CFString, multiOutUID: String) -> (OSStatus, AudioDeviceID) {
    let desc: [String : Any] = [
        kAudioAggregateDeviceNameKey: "My Multi-Output Device",
        kAudioAggregateDeviceUIDKey: multiOutUID,
        kAudioAggregateDeviceSubDeviceListKey: [[kAudioSubDeviceUIDKey: masterDeviceUID], [kAudioSubDeviceUIDKey: secondDeviceUID]],
        kAudioAggregateDeviceMasterSubDeviceKey: masterDeviceUID,
        kAudioAggregateDeviceIsStackedKey: 1,
        ]

    var aggregateDevice: AudioDeviceID = 0
    return (AudioHardwareCreateAggregateDevice(desc as CFDictionary, &aggregateDevice), aggregateDevice)
}

Too bad multi output devices don't have volume control, this makes them not so useful when they become the default output device.



来源:https://stackoverflow.com/questions/35469569/how-can-i-programmatically-create-a-multi-output-device-in-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!