public async void Stop(DialogViewModel viewModel, bool cancel) { Logger.Debug(Target.Recording, "Stop invoked, cancel: " + cancel); await _recordQueue.Enqueue(async() => { Logger.Debug(Target.Recording, "Enqueued stop invoked"); var recorder = _recorder; var file = _file; var mode = _mode; if (recorder == null || file == null) { Logger.Debug(Target.Recording, "recorder or file == null, abort"); return; } RecordingStopped?.Invoke(this, EventArgs.Empty); var now = DateTime.Now; var elapsed = now - _start; Logger.Debug(Target.Recording, "stopping recorder, elapsed " + elapsed); await recorder.StopAsync(); Logger.Debug(Target.Recording, "recorder stopped"); if (cancel || elapsed.TotalMilliseconds < 700) { try { await file.DeleteAsync(); } catch { } Logger.Debug(Target.Recording, "recording canceled or too short, abort"); if (elapsed.TotalMilliseconds < 700) { RecordingTooShort?.Invoke(this, EventArgs.Empty); } } else { Logger.Debug(Target.Recording, "sending recorded file"); Send(viewModel, mode, file, recorder._mirroringPreview, (int)elapsed.TotalSeconds); } _recorder = null; _file = null; }); }
private void Start() { _video = IsVideo; Task.Run(async() => { _startReset.WaitOne(); _startReset.Reset(); _recording = true; _start = DateTime.Now; Execute.BeginOnUIThread(() => { if (_video) { _roundView.IsOpen = true; } RecordingStarted?.Invoke(this, EventArgs.Empty); }); //_stopReset.Set(); //return; _file = await ApplicationData.Current.LocalFolder.CreateFileAsync(_video ? "temp\\recording.mp4" : "temp\\recording.ogg", CreationCollisionOption.ReplaceExisting); _recorder = new OpusRecorder(_file, _video); /* This following was moved from sub thread, because of a exception which comes from that device initializiation * is only allowed from UI thread! */ if (_recorder.m_mediaCapture != null) { Debug.WriteLine("Cannot start while recording"); } try { _recorder.m_mediaCapture = new MediaCapture(); var cameraDevice = await _recorder.FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Front); _recorder.settings.VideoDeviceId = cameraDevice.Id; await _recorder.m_mediaCapture.InitializeAsync(_recorder.settings); if (_video) { await _roundView.SetAsync(_recorder.m_mediaCapture); } await _recorder.StartAsync(); } catch (UnauthorizedAccessException) { Debug.WriteLine("The access to microphone was denied!"); return; } catch (Exception) { Debug.WriteLine("The app couldn't initialize microphone!"); return; } _pressed = true; _cancelOnRelease = false; _start = DateTime.Now; _stopReset.Set(); Debug.WriteLine("Start: " + _start); Debug.WriteLine("Stop unlocked"); }); }
public async void Start(ChatRecordMode mode) { Logger.Debug(Target.Recording, "Start invoked, mode: " + mode); await _recordQueue.Enqueue(async() => { Logger.Debug(Target.Recording, "Enqueued start invoked"); if (_recorder != null) { Logger.Debug(Target.Recording, "_recorder != null, abort"); RecordingFailed?.Invoke(this, EventArgs.Empty); return; } // Create a new temporary file for the recording var fileName = string.Format(mode == ChatRecordMode.Video ? "video_{0:yyyy}-{0:MM}-{0:dd}_{0:HH}-{0:mm}-{0:ss}.mp4" : "voice_{0:yyyy}-{0:MM}-{0:dd}_{0:HH}-{0:mm}-{0:ss}.ogg", DateTime.Now); var cache = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName); try { _mode = mode; _file = cache; _recorder = new OpusRecorder(cache, mode == ChatRecordMode.Video); _recorder.m_mediaCapture = new MediaCapture(); if (mode == ChatRecordMode.Video) { var cameraDevice = await _recorder.FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Front); if (cameraDevice == null) { // TODO: ... } // Figure out where the camera is located if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown) { // No information on the location of the camera, assume it's an external camera, not integrated on the device _recorder._externalCamera = true; } else { // Camera is fixed on the device _recorder._externalCamera = false; // Only mirror the preview if the camera is on the front panel _recorder._mirroringPreview = cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front; } _recorder.settings.VideoDeviceId = cameraDevice.Id; } await _recorder.m_mediaCapture.InitializeAsync(_recorder.settings); Logger.Debug(Target.Recording, "Devices initialized, starting"); await _recorder.StartAsync(); Logger.Debug(Target.Recording, "Recording started at " + DateTime.Now); _start = DateTime.Now; } catch (Exception ex) { Logger.Debug(Target.Recording, "Failed to initialize devices, abort: " + ex); _recorder.Dispose(); _recorder = null; _file = null; RecordingFailed?.Invoke(this, EventArgs.Empty); return; } RecordingStarted?.Invoke(this, EventArgs.Empty); }); }
private void Start() { _video = IsVideo; Task.Run(async() => { _startReset.WaitOne(); _startReset.Reset(); _recording = true; _start = DateTime.Now; this.BeginOnUIThread(() => { if (_video) { _roundView.IsOpen = true; } RecordingStarted?.Invoke(this, EventArgs.Empty); }); //_stopReset.Set(); //return; _file = await ApplicationData.Current.LocalFolder.CreateFileAsync(_video ? "temp\\recording.mp4" : "temp\\recording.ogg", CreationCollisionOption.ReplaceExisting); _recorder = new OpusRecorder(_file, _video); /* This following was moved from sub thread, because of a exception which comes from that device initializiation * is only allowed from UI thread! */ if (_recorder.m_mediaCapture != null) { Debug.WriteLine("Cannot start while recording"); } try { _recorder.m_mediaCapture = new MediaCapture(); var cameraDevice = await _recorder.FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Front); if (cameraDevice == null) { // TODO: ... } // Figure out where the camera is located if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown) { // No information on the location of the camera, assume it's an external camera, not integrated on the device _recorder._externalCamera = true; } else { // Camera is fixed on the device _recorder._externalCamera = false; // Only mirror the preview if the camera is on the front panel _recorder._mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front); } _recorder.settings.VideoDeviceId = cameraDevice.Id; await _recorder.m_mediaCapture.InitializeAsync(_recorder.settings); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { if (_video) { // Initialize rotationHelper _recorder._rotationHelper = new CameraRotationHelper(cameraDevice.EnclosureLocation); _recorder._rotationHelper.OrientationChanged += RotationHelper_OrientationChanged; await _roundView.SetAsync(_recorder.m_mediaCapture, _recorder._mirroringPreview); await _recorder.SetPreviewRotationAsync(); } }); await _recorder.StartAsync(); } catch (UnauthorizedAccessException) { Debug.WriteLine("The access to microphone was denied!"); return; } catch (Exception) { Debug.WriteLine("The app couldn't initialize microphone!"); return; } _pressed = true; _cancelOnRelease = false; _start = DateTime.Now; _stopReset.Set(); Debug.WriteLine("Start: " + _start); Debug.WriteLine("Stop unlocked"); }); }