forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioCapturePermissions.cs
More file actions
81 lines (77 loc) · 3.87 KB
/
AudioCapturePermissions.cs
File metadata and controls
81 lines (77 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Threading.Tasks;
using Windows.Media.Capture;
namespace SDKTemplate
{
public class AudioCapturePermissions
{
// If no recording device is attached, attempting to get access to audio capture devices will throw
// a System.Exception object, with this HResult set.
private static int NoCaptureDevicesHResult = -1072845856;
/// <summary>
/// On desktop/tablet systems, users are prompted to give permission to use capture devices on a
/// per-app basis. Along with declaring the microphone DeviceCapability in the package manifest,
/// this method tests the privacy setting for microphone access for this application.
/// Note that this only checks the Settings->Privacy->Microphone setting, it does not handle
/// the Cortana/Dictation privacy check, however (Under Settings->Privacy->Speech, Inking and Typing).
///
/// Developers should ideally perform a check like this every time their app gains focus, in order to
/// check if the user has changed the setting while the app was suspended or not in focus.
/// </summary>
/// <returns>true if the microphone can be accessed without any permissions problems.</returns>
public async static Task<bool> RequestMicrophonePermission()
{
try
{
// Request access to the microphone only, to limit the number of capabilities we need
// to request in the package manifest.
MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
settings.MediaCategory = MediaCategory.Speech;
MediaCapture capture = new MediaCapture();
await capture.InitializeAsync(settings);
}
catch (TypeLoadException)
{
// On SKUs without media player (eg, the N SKUs), we may not have access to the Windows.Media.Capture
// namespace unless the media player pack is installed. Handle this gracefully.
var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components are unavailable.");
await messageDialog.ShowAsync();
return false;
}
catch (UnauthorizedAccessException)
{
// The user has turned off access to the microphone. If this occurs, we should show an error, or disable
// functionality within the app to ensure that further exceptions aren't generated when
// recognition is attempted.
return false;
}
catch (Exception exception)
{
// This can be replicated by using remote desktop to a system, but not redirecting the microphone input.
// Can also occur if using the virtual machine console tool to access a VM instead of using remote desktop.
if (exception.HResult == NoCaptureDevicesHResult)
{
var messageDialog = new Windows.UI.Popups.MessageDialog("No Audio Capture devices are present on this system.");
await messageDialog.ShowAsync();
return false;
}
else
{
throw;
}
}
return true;
}
}
}