public static void Init() { try { var src = ConvertToUInt("dOut"); uint theRunLoop = 0; var property = new AudioObjectPropertyAddress( kAudioHardwarePropertyDefaultOutputDevice, (uint)AudioObjectPropertyScope.Global, (uint)AudioObjectPropertyElement.Master); AudioObjectAddPropertyListener(1, ref property, OutputDidChange, IntPtr.Zero); //var defaultDevice = GetCurrentOutputDevice(); //// var name = GetDeviceName (defaultDevice); //var sourceId = GetDeviceSourceId(defaultDevice); //// var trasnport = ConvertToString (GetDeviceProperty (defaultDevice, AudioDevicePropertyTransportType)); //var theAddress = new AudioObjectPropertyAddress( // AudioDevicePropertyDataSource, // (uint)AudioObjectPropertyScope.Output, // (uint)AudioObjectPropertyElement.Master); //AudioObjectAddPropertyListener(1, ref theAddress, OutputDidChange, IntPtr.Zero); } catch (Exception ex) { Console.WriteLine(ex); } }
static extern int AudioObjectSetPropertyData( uint inObjectID, ref AudioObjectPropertyAddress inAddress, ref uint inQualifierDataSize, ref IntPtr inQualifierData, ref uint ioDataSize, ref uint inData );
public static uint GetDeviceId(uint deviceId) { uint dataSourceId; uint size = (uint)Marshal.SizeOf(typeof(uint)); var theAddress = new AudioObjectPropertyAddress( AudioDevicePropertyDataSource, (uint)AudioObjectPropertyScope.Output, (uint)AudioObjectPropertyElement.Master); uint inQualifierDataSize = 0; IntPtr inQualifierData = IntPtr.Zero; var err = AudioObjectGetPropertyData(deviceId, ref theAddress, ref inQualifierDataSize, ref inQualifierData, ref size, out dataSourceId); //if (err != 0) //throw new AudioUnitException((int)err); return(dataSourceId); }
static bool SetDeviceProperty(AudioObjectPropertyAddress theAddress, uint deviceId, uint data) { uint dataSourceId = data; uint size = (uint)Marshal.SizeOf(typeof(UInt64)); uint inQualifierDataSize = 0; IntPtr inQualifierData = IntPtr.Zero; var err = AudioObjectSetPropertyData(deviceId, ref theAddress, ref inQualifierDataSize, ref inQualifierData, ref size, ref dataSourceId); if (err != 0) { var error = new NSError(NSError.OsStatusErrorDomain, err); Console.WriteLine(error); return(false); } return(true);; }
public static uint GetDeviceProperty(uint deviceId, uint property) { uint dataSourceId; uint size = (uint)Marshal.SizeOf(typeof(UInt64)); var theAddress = new AudioObjectPropertyAddress( property, (uint)AudioObjectPropertyScope.Output, (uint)AudioObjectPropertyElement.Master); uint inQualifierDataSize = 0; IntPtr inQualifierData = IntPtr.Zero; var err = AudioObjectGetPropertyData(deviceId, ref theAddress, ref inQualifierDataSize, ref inQualifierData, ref size, out dataSourceId); if (err != 0) { var error = new NSError(NSError.OsStatusErrorDomain, err); Console.WriteLine(error); } return(dataSourceId); }
public static uint GetCurrentOutputDevice() { uint outputDevice; uint size = (uint)Marshal.SizeOf(typeof(uint)); var theAddress = new AudioObjectPropertyAddress( AudioObjectPropertySelector.DefaultSystemOutputDevice, AudioObjectPropertyScope.Global, AudioObjectPropertyElement.Master); uint inQualifierDataSize = 0; IntPtr inQualifierData = IntPtr.Zero; var err = AudioObjectGetPropertyData(1, ref theAddress, ref inQualifierDataSize, ref inQualifierData, ref size, out outputDevice); if (err != 0) { var status = (AudioUnitStatus)err; Console.WriteLine(status); } return(outputDevice); }
static extern int AudioObjectAddPropertyListener(uint inObjectID, ref AudioObjectPropertyAddress inAddress, OutputChange callback, IntPtr clientData);
static void OutputDidChange(uint inObjectID, uint inNumberAddresses, AudioObjectPropertyAddress inAddresses, IntPtr clientData) { Console.WriteLine("Output Changed"); OutputChanged?.Invoke(); }