2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
20 namespace Tizen.Multimedia
23 /// Provides the ability to control volume levels and monitor audio devices.
25 public static class AudioManager
29 VolumeController = new AudioVolume();
33 /// Gets the volume controller.
35 /// <value>The <see cref="AudioVolume"/>.</value>
36 public static AudioVolume VolumeController { get; }
39 /// Gets the all devices currently connected.
41 /// <returns>An IEnumerable<AudioDevice> that contains connected devices.</returns>
42 public static IEnumerable<AudioDevice> GetConnectedDevices()
44 IntPtr deviceListHandle = IntPtr.Zero;
48 var ret = Interop.AudioDevice.GetDeviceList(AudioDeviceOptions.All, out deviceListHandle);
50 List<AudioDevice> result = new List<AudioDevice>();
52 if (ret == AudioManagerError.NoData)
57 ret.Validate("Failed to get connected devices");
59 while (ret == AudioManagerError.None)
61 ret = Interop.AudioDevice.GetNextDevice(deviceListHandle, out var deviceHandle);
63 if (ret == AudioManagerError.NoData)
68 ret.Validate("Failed to get connected devices");
70 result.Add(new AudioDevice(deviceHandle));
76 Interop.AudioDevice.FreeDeviceList(deviceListHandle);
80 #region DeviceConnectionChanged event
81 private static int _deviceConnectionChangedCallbackId = -1;
83 private static Interop.AudioDevice.ConnectionChangedCallback _audioDeviceConnectionChangedCallback;
84 private static EventHandler<AudioDeviceConnectionChangedEventArgs> _audioDeviceConnectionChanged;
85 private static object _audioDeviceConnectionLock = new object();
88 /// Occurs when the state of connection of an audio device changes.
90 public static event EventHandler<AudioDeviceConnectionChangedEventArgs> DeviceConnectionChanged
94 lock (_audioDeviceConnectionLock)
96 if (_audioDeviceConnectionChanged == null)
98 RegisterAudioDeviceEvent();
100 _audioDeviceConnectionChanged += value;
110 lock (_audioDeviceConnectionLock)
112 if (_audioDeviceConnectionChanged == value)
114 UnregisterDeviceConnectionChangedEvent();
116 _audioDeviceConnectionChanged -= value;
121 private static void RegisterAudioDeviceEvent()
123 _audioDeviceConnectionChangedCallback = (IntPtr device, bool isConnected, IntPtr userData) =>
125 _audioDeviceConnectionChanged?.Invoke(null,
126 new AudioDeviceConnectionChangedEventArgs(new AudioDevice(device), isConnected));
129 Interop.AudioDevice.AddDeviceConnectionChangedCallback(AudioDeviceOptions.All,
130 _audioDeviceConnectionChangedCallback, IntPtr.Zero, out _deviceConnectionChangedCallbackId).
131 Validate("Unable to add device connection changed callback");
134 private static void UnregisterDeviceConnectionChangedEvent()
136 Interop.AudioDevice.RemoveDeviceConnectionChangedCallback(_deviceConnectionChangedCallbackId).
137 Validate("Unable to remove device connection changed callback");
141 #region DeviceStateChanged event
142 private static int _deviceStateChangedCallbackId = -1;
144 private static Interop.AudioDevice.StateChangedCallback _audioDeviceStateChangedCallback;
145 private static EventHandler<AudioDeviceStateChangedEventArgs> _audioDeviceStateChanged;
146 private static object _audioDeviceStateLock = new object();
149 /// Occurs when the state of an audio device changes.
151 public static event EventHandler<AudioDeviceStateChangedEventArgs> DeviceStateChanged
155 lock (_audioDeviceStateLock)
157 if (_audioDeviceStateChanged == null)
159 RegisterDeviceStateChangedEvent();
161 _audioDeviceStateChanged += value;
171 lock (_audioDeviceStateLock)
173 if (_audioDeviceStateChanged == value)
175 UnregisterDeviceStateChangedEvent();
177 _audioDeviceStateChanged -= value;
182 private static void RegisterDeviceStateChangedEvent()
184 _audioDeviceStateChangedCallback = (IntPtr device, AudioDeviceState changedState, IntPtr userData) =>
186 _audioDeviceStateChanged?.Invoke(null,
187 new AudioDeviceStateChangedEventArgs(new AudioDevice(device), changedState));
190 Interop.AudioDevice.AddDeviceStateChangedCallback(AudioDeviceOptions.All,
191 _audioDeviceStateChangedCallback, IntPtr.Zero, out _deviceStateChangedCallbackId).
192 Validate("Failed to add device state changed event");
195 private static void UnregisterDeviceStateChangedEvent()
197 Interop.AudioDevice.RemoveDeviceStateChangedCallback(_deviceStateChangedCallbackId).
198 Validate("Failed to remove device state changed event");