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 /// <since_tizen> 3 </since_tizen>
26 public static class AudioManager
30 VolumeController = new AudioVolume();
34 /// Gets the volume controller.
36 /// <value>The <see cref="AudioVolume"/>.</value>
37 /// <since_tizen> 3 </since_tizen>
38 public static AudioVolume VolumeController { get; }
41 /// Gets the all devices currently connected.
43 /// <returns>An IEnumerable<AudioDevice> that contains connected devices.</returns>
44 /// <since_tizen> 4 </since_tizen>
45 public static IEnumerable<AudioDevice> GetConnectedDevices()
47 IntPtr deviceListHandle = IntPtr.Zero;
51 var ret = Interop.AudioDevice.GetDeviceList(AudioDeviceOptions.All, out deviceListHandle);
53 List<AudioDevice> result = new List<AudioDevice>();
55 if (ret == AudioManagerError.NoData)
60 ret.Validate("Failed to get connected devices");
62 while (ret == AudioManagerError.None)
64 ret = Interop.AudioDevice.GetNextDevice(deviceListHandle, out var deviceHandle);
66 if (ret == AudioManagerError.NoData)
71 ret.Validate("Failed to get connected devices");
73 result.Add(new AudioDevice(deviceHandle));
79 Interop.AudioDevice.FreeDeviceList(deviceListHandle);
83 #region DeviceConnectionChanged event
84 private static int _deviceConnectionChangedCallbackId = -1;
86 private static Interop.AudioDevice.ConnectionChangedCallback _audioDeviceConnectionChangedCallback;
87 private static EventHandler<AudioDeviceConnectionChangedEventArgs> _audioDeviceConnectionChanged;
88 private static object _audioDeviceConnectionLock = new object();
91 /// Occurs when the state of a connection of an audio device changes.
93 /// <since_tizen> 3 </since_tizen>
94 public static event EventHandler<AudioDeviceConnectionChangedEventArgs> DeviceConnectionChanged
98 lock (_audioDeviceConnectionLock)
100 if (_audioDeviceConnectionChanged == null)
102 RegisterAudioDeviceEvent();
104 _audioDeviceConnectionChanged += value;
114 lock (_audioDeviceConnectionLock)
116 if (_audioDeviceConnectionChanged == value)
118 UnregisterDeviceConnectionChangedEvent();
120 _audioDeviceConnectionChanged -= value;
125 private static void RegisterAudioDeviceEvent()
127 _audioDeviceConnectionChangedCallback = (IntPtr device, bool isConnected, IntPtr userData) =>
129 _audioDeviceConnectionChanged?.Invoke(null,
130 new AudioDeviceConnectionChangedEventArgs(new AudioDevice(device), isConnected));
133 Interop.AudioDevice.AddDeviceConnectionChangedCallback(AudioDeviceOptions.All,
134 _audioDeviceConnectionChangedCallback, IntPtr.Zero, out _deviceConnectionChangedCallbackId).
135 Validate("Unable to add device connection changed callback");
138 private static void UnregisterDeviceConnectionChangedEvent()
140 Interop.AudioDevice.RemoveDeviceConnectionChangedCallback(_deviceConnectionChangedCallbackId).
141 Validate("Unable to remove device connection changed callback");
145 #region DeviceStateChanged event
146 private static int _deviceStateChangedCallbackId = -1;
148 private static Interop.AudioDevice.StateChangedCallback _audioDeviceStateChangedCallback;
149 private static EventHandler<AudioDeviceStateChangedEventArgs> _audioDeviceStateChanged;
150 private static object _audioDeviceStateLock = new object();
153 /// Occurs when the state of an audio device changes.
155 /// <since_tizen> 3 </since_tizen>
156 public static event EventHandler<AudioDeviceStateChangedEventArgs> DeviceStateChanged
160 lock (_audioDeviceStateLock)
162 if (_audioDeviceStateChanged == null)
164 RegisterDeviceStateChangedEvent();
166 _audioDeviceStateChanged += value;
176 lock (_audioDeviceStateLock)
178 if (_audioDeviceStateChanged == value)
180 UnregisterDeviceStateChangedEvent();
182 _audioDeviceStateChanged -= value;
187 private static void RegisterDeviceStateChangedEvent()
189 _audioDeviceStateChangedCallback = (IntPtr device, AudioDeviceState changedState, IntPtr userData) =>
191 _audioDeviceStateChanged?.Invoke(null,
192 new AudioDeviceStateChangedEventArgs(new AudioDevice(device), changedState));
195 Interop.AudioDevice.AddDeviceStateChangedCallback(AudioDeviceOptions.All,
196 _audioDeviceStateChangedCallback, IntPtr.Zero, out _deviceStateChangedCallbackId).
197 Validate("Failed to add device state changed event");
200 private static void UnregisterDeviceStateChangedEvent()
202 Interop.AudioDevice.RemoveDeviceStateChangedCallback(_deviceStateChangedCallbackId).
203 Validate("Failed to remove device state changed event");