protected override async Task CloseMayOverrideAsync() { await StopRecordingAsync().ConfigureAwait(false); SemaphoreSlimSafeRelease.TryDispose(_recordLockingSemaphore); _recordLockingSemaphore = null; var mc = _mediaCapture; if (mc != null) { mc.Failed -= OnMediaCapture_Failed; try { mc.Dispose(); } catch { } } _mediaCapture = null; var audioRecorder = _audioRecorder; if (audioRecorder != null) { audioRecorder.UnrecoverableError -= OnAudioRecorder_UnrecoverableError; await audioRecorder.CloseAsync(); } _audioRecorder = null; Task stopAllAnims = RunInUiThreadAsync(delegate { RecordingStoryboard.SkipToFill(); RecordingStoryboard.Stop(); FailureStoryboard.SkipToFill(); FailureStoryboard.Stop(); }); }
private async Task<bool> StartRecordingAsync(StorageFile file) { bool isOk = false; Task stb = RunInUiThreadAsync(delegate { RecordingStoryboard.Begin(); }); try { _audioRecorder = new AudioRecorder(this, file); _audioRecorder.UnrecoverableError += OnAudioRecorder_UnrecoverableError; await _audioRecorder.OpenAsync(); // adjust the microphone volume. You need MediaCapture, apparently, and she needs STAThread. Ridiculous. _mediaCapture = new MediaCapture(); // LOLLO NOTE the following fails with the phone // var settings = new MediaCaptureInitializationSettings { AudioDeviceId = RuntimeData.Instance?.AudioDevice?.Id, MediaCategory = MediaCategory.Other, StreamingCaptureMode = StreamingCaptureMode.Audio }; // the following works with the phone var settings = new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Audio }; _mediaCapture.Failed += OnMediaCapture_Failed; await _mediaCapture.InitializeAsync(settings); _mediaCapture.AudioDeviceController.Muted = false; _mediaCapture.AudioDeviceController.VolumePercent = (float)VolumeSlider.Value; //_mediaCapture.AudioDeviceController.VolumePercent = 100.0F; // The following is useless, it was a feeble attempt at getting a graphical display of audio levels. It fails, don't use it. //await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, delegate { PreviewControl.Source = _mediaCapture; }); //await _mediaCapture.StartPreviewAsync(); IsRecording = true; isOk = await _audioRecorder.StartRecordingAsync().ConfigureAwait(false); if (!isOk) { await Logger.AddAsync("AudioRecordingCannotStart", Logger.ForegroundLogFilename); NotifyOfFailure(RuntimeData.GetText("AudioRecordingCannotStart")); } } catch (Exception ex) { isOk = false; await Logger.AddAsync(ex.ToString(), Logger.ForegroundLogFilename); NotifyOfFailure(ex.Message); } return isOk; }