private AudioCaptureControl() { this.mediaCaptureSemaphore = new SemaphoreSlim(1, 1); // We will query and monitor the microphone capability for audio input; the call to RequestAccessAsync needs to occur once we've // created a page so it can pop a consent prompt. this.MicrophoneCapability = AppCapability.Create("microphone"); this.MicrophoneCapability.AccessChanged += (capability, args) => { if (capability.CheckAccess() == AppCapabilityAccessStatus.Allowed) { _ = this.StartVolumePollingAsync(); } }; // We'll also monitor the audio input devices available on the system. This allows clear feedback to users when no device is connected // as well as responsiveness to devices being added or removed. this.captureDeviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.AudioCapture); this.captureDeviceWatcher.Added += async(s, e) => await this.OnAudioCaptureDevicesChangedAsync(); this.captureDeviceWatcher.Removed += async(s, e) => await this.OnAudioCaptureDevicesChangedAsync(); this.captureDeviceWatcher.Updated += async(s, e) => await this.OnAudioCaptureDevicesChangedAsync(); this.enumerationCompletedEvent = new ManualResetEventSlim(false); this.captureDeviceWatcher.EnumerationCompleted += (s, e) => { this.firstEnumerationCompleted = true; this.enumerationCompletedEvent.Set(); }; this.captureDeviceWatcher.Start(); }
async void RequestAccessButton_Click(object sender, RoutedEventArgs e) { // This will prompt for any capabilities whose current status is UserPromptRequired. IReadOnlyDictionary <string, AppCapabilityAccessStatus> result = await AppCapability.RequestAccessForCapabilitiesAsync(new[] { "location", "webcam" }); string locationMessage; if (result["location"] == AppCapabilityAccessStatus.Allowed) { locationMessage = "Location access is granted. "; } else { // We could inspect the specific status to provide better feedback, // but for the purpose of this sample, we merely report whether access was granted // and do not study the reason why. locationMessage = "Location access is not granted. "; } string webcamMessage; if (result["webcam"] == AppCapabilityAccessStatus.Allowed) { webcamMessage = "Webcam access is granted. "; } else { // We could inspect the specific status to provide better feedback, // but for the purpose of this sample, we merely report whether access was granted // and do not study the reason why. webcamMessage = "Webcam access is not granted. "; } rootPage.NotifyUser(locationMessage + webcamMessage, NotifyType.StatusMessage); }
protected override void OnNavigatedTo(NavigationEventArgs e) { // Specify which capability we want to query/monitor. locationCapability = AppCapability.Create("location"); // Register a handler to be called when access changes. locationCapability.AccessChanged += OnCapabilityAccessChanged; // Update UI to match current access. UpdateCapabilityStatus(); }
async void OnCapabilityAccessChanged(AppCapability sender, object e) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateCapabilityStatus()); }