AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
MicrophoneHelper.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 //*********************************************************
5 //
6 // This sample code is a modified version of an official UWP sample which can be found on:
7 // https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/SpeechRecognitionAndSynthesis/cs/AudioCapturePermissions.cs
8 //
9 //*********************************************************
10 
11 #if ENABLE_WINMD_SUPPORT
12 using System;
13 using System.Threading.Tasks;
14 using Windows.Media.Capture;
15 #endif
16 
17 namespace HoloToolkit.Unity
18 {
19  public enum MicrophoneStatus
20  {
27  }
28 
38  public class MicrophoneHelper
39  {
40 #if ENABLE_WINMD_SUPPORT
41 
49  private static int NoCaptureDevicesHResult = -1072845856;
50 
65  public async static Task<MicrophoneStatus> GetMicrophoneStatus()
66  {
67  // Request access to the microphone only, to limit the number of capabilities we need
68  // to request in the package manifest.
69  MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
70  settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
71  settings.MediaCategory = MediaCategory.Speech;
72  MediaCapture capture = new MediaCapture();
73  try
74  {
75  // If this is the first time we run the app, this will show the
76  // microphone permissions dialog, where the user is asked if they
77  // allow the app to use the microphone. If the dialog is dismissed, or not
78  // accepted, the app will not ask for permissions again. The user will need
79  // to go to Settings->Privacy->Microphone Settings to allow the app to
80  // use the microphone
81  await capture.InitializeAsync(settings);
82  }
83  catch (TypeLoadException)
84  {
85  // On SKUs without media player (eg, the N SKUs), we may not have access to the Windows.Media.Capture
86  // namespace unless the media player pack is installed.
87  return MicrophoneStatus.MediaComponentsMissing;
88  }
89  catch (UnauthorizedAccessException)
90  {
91  // The user has turned off access to the microphone. If this occurs, we should show an error, or disable
92  // functionality within the app to ensure that further exceptions aren't generated when
93  // recognition is attempted.
94  return MicrophoneStatus.MicrophoneUseNotAuthorized;
95  }
96  catch (Exception exception)
97  {
98  // In mixed reality this would be the case when your HMD uses headset jack, your host machine does not have connected microphone
99  // (i.e. desktop with no integrated mic, nothing plugged into mic/headset jack or you disabled all recording endpoints)
100  // If the HMD has built in microphones, they would be disabled when HMD is not worn and will be enabled when you put the device on.
101  if (exception.HResult == NoCaptureDevicesHResult)
102  {
103  return MicrophoneStatus.MicrophoneNotPresent;
104  }
105  else
106  {
107  return MicrophoneStatus.MicrophoneUseFailed;
108  }
109  }
110 
111  // if microphone is muted, speech recognition will not hear anything.
112  // return appropriate value to indicate that.
113  if(capture.AudioDeviceController.Muted)
114  {
115  return MicrophoneStatus.MicrophoneMuted;
116  }
117 
118  return MicrophoneStatus.MicrophoneReady;
119  }
120 
128  public static async Task<bool> IsMicrophoneReady()
129  {
130  var status = await GetMicrophoneStatus();
131  return status == MicrophoneStatus.MicrophoneReady;
132  }
133 #else
134  public static MicrophoneStatus GetMicrophoneStatus()
141  {
142  return MicrophoneStatus.MicrophoneReady;
143  }
144 
148  public static bool IsMicrophoneReady()
149  {
150  var status = GetMicrophoneStatus();
151  return status == MicrophoneStatus.MicrophoneReady;
152  }
153 #endif
154  }
155 }
Provides a working status information of the default microphone on the system. Class is using UWP API...
static bool IsMicrophoneReady()
Stub function imitating the original IsMicrophoneReady