private async void buttonPreviewClick(object sender, RoutedEventArgs e) { // 静止画撮影、動画録画をおこなうキャプチャーオブジェクト var _capture = new Windows.Media.Capture.MediaCapture(); try { // Webカメラの初期化 await _capture.InitializeAsync(); } catch (UnauthorizedAccessException ex) { // "アクセスが拒否されました。 (HRESULT からの例外: 0x80070005 (E_ACCESSDENIED))" // Web カメラがユーザーに許可されていないと、 // UnauthorizedAccessException が出る。 ShowRequestMessageAsync(ex); return; } // MediaCapture インスタンスを CaptureElement コントロールに設定する capturePreview.Source = _capture; // プレビューを開始する await _capture.StartPreviewAsync(); }
async private void GetPreview() { Windows.Media.Capture.MediaCapture takePhotoManager = new Windows.Media.Capture.MediaCapture(); await takePhotoManager.InitializeAsync(); // start previewing PhotoPreview.Source = takePhotoManager; await takePhotoManager.StartPreviewAsync(); // to stop it await takePhotoManager.StopPreviewAsync(); ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg(); // a file to save a photo StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync( "Photo.jpg", CreationCollisionOption.ReplaceExisting); await takePhotoManager.CapturePhotoToStorageFileAsync(imgFormat, file); // Get photo as a BitmapImage BitmapImage bmpImage = new BitmapImage(new Uri(file.Path)); // imagePreivew is a <Image> object defined in XAML imagePreivew.Source = bmpImage; }
private async System.Threading.Tasks.Task <bool> InitializeRecording() { isRecordingInitialized = false; try { // Initialize MediaCapture mediaCapture = new Windows.Media.Capture.MediaCapture(); await mediaCapture.InitializeAsync(new Windows.Media.Capture.MediaCaptureInitializationSettings { //VideoSource = screenCapture.VideoSource, // AudioSource = screenCapture.AudioSource, StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio, MediaCategory = Windows.Media.Capture.MediaCategory.Other, AudioProcessing = Windows.Media.AudioProcessing.Raw }); mediaCapture.RecordLimitationExceeded += mediaCapture_RecordLimitationExceeded; mediaCapture.Failed += mediaCapture_Failed; System.Diagnostics.Debug.WriteLine("Device Initialized Successfully..."); isRecordingInitialized = true; } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception while initializing the device: " + e.Message); } return(isRecordingInitialized); }
private async void EnableMicrophoneButton_Click(object sender, RoutedEventArgs e) { bool isMicAvailable = true; try { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); } catch (Exception) { isMicAvailable = false; } if (!isMicAvailable) { await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone")); } else { NotifyUser("Microphone was enabled", NotifyType.StatusMessage); this.EnableMicrophoneButton.IsEnabled = false; this.RecognizeKeywordButton.IsEnabled = true; } }
private async Task RestartPreviewAsync() { Windows.Media.Capture.MediaCapture capture = capturePreview.Source; if (capture == null) { return; //以前はキャプチャしていなかった } // キャプチャオブジェクトを作り直し capture = new Windows.Media.Capture.MediaCapture(); try { // Webカメラの初期化 await capture.InitializeAsync(); // 作り直した MediaCapture インスタンスを CaptureElement コントロールに設定する capturePreview.Source = capture; // 本当はフィルタも再設定しなければいけない // プレビューを再開する await capture.StartPreviewAsync(); } catch { //(void) } }
public static async Task EnableMicrophone() { if (IsMicAvailable) { return; } IsMicAvailable = true; try { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); } catch (Exception) { IsMicAvailable = false; } if (!IsMicAvailable) { await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone")); } else { Debug.WriteLine("Microphone was enabled"); } }
private static async Task EnsureMicIsEnabled() { bool isMicAvailable = true; try { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); } catch (Exception) { isMicAvailable = false; } if (!isMicAvailable) { await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone")); } else { Trace.WriteLine("Microphone already enabled"); } }
// Check if the app has mic permissions. If not, query user for access private async Task CheckAndEnableMic() { bool isMicAvailable = true; try { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); } catch (Exception) { isMicAvailable = false; } if (!isMicAvailable) { await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone")); } else { UpdateUI(() => { this.Messages.Add(new MessageDisplay("Microphone enabled", Sender.Other)); }); } }
public async Task <bool> GetPermissionsAsync() { var isMicAvailable = true; try { using (var mediaCapture = new Windows.Media.Capture.MediaCapture()) { var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings { StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio }; await mediaCapture.InitializeAsync(settings); } } catch (Exception) { isMicAvailable = false; } if (!isMicAvailable) { await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone")); } return(isMicAvailable); }
/// <summary> /// Cleans up the microphone resources and the stream and unregisters from MediaCapture events /// </summary> /// <returns>true if successful</returns> public async System.Threading.Tasks.Task <bool> CleanupRecording() { if (isRecordingInitialized) { // If a recording is in progress during cleanup, stop it to save the recording if (isRecording) { await StopRecording(); } isRecordingInitialized = false; } if (mediaCapture != null) { mediaCapture.RecordLimitationExceeded -= mediaCapture_RecordLimitationExceeded; mediaCapture.Failed -= mediaCapture_Failed; mediaCapture.Dispose(); mediaCapture = null; } if (STTStream != null) { STTStream.BufferReady -= STTStream_BufferReady; STTStream.AudioLevel -= STTStream_AudioLevel; STTStream.Dispose(); STTStream = null; } return(true); }
private async void buttonPreviewWithVideoStabilizationClick(object sender, RoutedEventArgs e) { var _capture = new Windows.Media.Capture.MediaCapture(); try { await _capture.InitializeAsync(); } catch (UnauthorizedAccessException ex) { ShowRequestMessageAsync(ex); return; } // キャプチャしたビデオに手ブレ補正効果を追加する // http://msdn.microsoft.com/ja-jp/library/windows/apps/xaml/hh868169.aspx await _capture.AddEffectAsync( Windows.Media.Capture.MediaStreamType.VideoRecord, Windows.Media.VideoEffects.VideoStabilization, null); capturePreview.Source = _capture; await _capture.StartPreviewAsync(); }
private static async Task SetupCapturMedia() { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); }
private async void buttonTakePhotoClick(object sender, RoutedEventArgs e) { if (capturePreview.Source == null) { return; } // プレビューに付けた MediaCapture オブジェクトを取り出す Windows.Media.Capture.MediaCapture capture = capturePreview.Source; // 保存する画像ファイルのフォーマット (pngファイルを指定) var imageProperties = Windows.Media.MediaProperties.ImageEncodingProperties.CreatePng(); // 撮影する。撮影した画像の stream が返ってくる。 // 自前のコードで WriteableBitmap に変換 using (var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { try { await capture.CapturePhotoToStreamAsync(imageProperties, memStream); // キャプチャ開始から撮影までの間に許可を取り消されると、ここで例外が出る // 「指定されたファイル ハンドルへのアクセスが取り消されました。 (HRESULT からの例外: 0x80070326)」 } catch (Exception ex) { ShowRequestMessageAsync(ex); return; } captureImage.Source = await memStream.ToWriteableBitmap(); } // 撮影する。素直にいったんファイルに落とす方法 //StorageFile file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("test.png", CreationCollisionOption.GenerateUniqueName); //try //{ // await capture.CapturePhotoToStorageFileAsync(imageProperties, file); // // キャプチャ開始から撮影までの間に許可を取り消されると、ここで例外が出る // // 「指定されたファイル ハンドルへのアクセスが取り消されました。 (HRESULT からの例外: 0x80070326)」 //} //catch (Exception ex) //{ // ShowRequestMessageAsync(ex); // return; //} //using (var stream = await file.OpenReadAsync()) //{ // var bi = new BitmapImage(); // bi.SetSource(stream); // captureImage.Source = bi; //} }
private async void SetupMicrophone() { // Set default microphone here, so we can get the usb mic not the webcam mic // Enables camera operations with PO captureDev = new Windows.Media.Capture.MediaCapture(); await captureDev.InitializeAsync(); microphone = captureDev.AudioDeviceController; }
async void mediaCapture_Failed(Windows.Media.Capture.MediaCapture sender, Windows.Media.Capture.MediaCaptureFailedEventArgs errorEventArgs) { System.Diagnostics.Debug.WriteLine("Fatal Error " + errorEventArgs.Message); await StopRecording(); if (AudioCaptureError != null) { AudioCaptureError(this, errorEventArgs.Message); } }
async void mediaCapture_RecordLimitationExceeded(Windows.Media.Capture.MediaCapture sender) { System.Diagnostics.Debug.WriteLine("Stopping Record on exceeding max record duration"); await StopRecording(); if (AudioCaptureError != null) { AudioCaptureError(this, "Error Media Capture: Record Limitation Exceeded"); } }
private async void EnableMicrophone() { try { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); } catch (Exception) { MyMessageBox("No se pudo inicializar el micrófono"); } }
private async Task CheckMicSettings() { try { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); } catch (Exception exp) { LogOutput($"Error initialing mic: {exp}"); } }
private int intDelay = 200; // Record Time in millesecond... // Short time - increase repetet noise ... // Long time - create echo ... // I think 200ms is optimum ... // If you use headphones - best time will be 500..1000ms ... private async void btnStart_Click(object sender, RoutedEventArgs e) { blnStart = true; btnStart.Visibility = Visibility.Collapsed; btnStop.Visibility = Visibility.Visible; textBlock.Visibility = Visibility.Visible; mediaCaptureAudioPrimery = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; settings.MediaCategory = Windows.Media.Capture.MediaCategory.Other; settings.AudioProcessing = Windows.Media.AudioProcessing.Default; // Use only Default await mediaCaptureAudioPrimery.InitializeAsync(settings); recordProfile = MediaEncodingProfile.CreateWav(Windows.Media.MediaProperties.AudioEncodingQuality.Low); while (blnStart) // Repeate untile stop ... { try { msIRAS0 = new MemoryStream(); streamIRAS0 = msIRAS0.AsRandomAccessStream(); // New Stream ... await mediaCaptureAudioPrimery.StartRecordToStreamAsync(recordProfile, streamIRAS0); // write audio in first stream ... await Task.Delay(intDelay); await mediaCaptureAudioPrimery.StopRecordAsync(); // Stop first stream await PlayThreadMethod(streamIRAS0); // Play from first stream msIRAS1 = new MemoryStream(); streamIRAS1 = msIRAS0.AsRandomAccessStream(); // Second Stream ... await mediaCaptureAudioPrimery.StartRecordToStreamAsync(recordProfile, streamIRAS1); // sweetch stream ... to second stream ... await Task.Delay(intDelay); await mediaCaptureAudioPrimery.StopRecordAsync(); await PlayThreadMethod(streamIRAS1); // Play Second Streem } catch (Exception ex) { Stop(); } } }
internal async Task initializeCamera() { try { if (!initialized) { m_mediaCaptureMgr = new Windows.Media.Capture.MediaCapture(); await m_mediaCaptureMgr.InitializeAsync(); if (debug) statusListBox.Items.Add("Device initialized successful"); initialized = true; } } catch (Exception exception) { if (debug) statusListBox.Items.Add("error initialize"); } }
private async Task <bool> GetPermissions() { #if WINDOWS_UWP try { var capture = new Windows.Media.Capture.MediaCapture(); await capture.InitializeAsync(); Debug.Log("Camera and Microphone permissions OK"); return(true); } catch (UnauthorizedAccessException) { Debug.LogError("Camera and microphone permissions not granted."); return(false); } #else // WINDOWS_UWP SimpleConsole.AddLine(log, $"Not setup for WINDOWS_UWP"); await Task.CompletedTask; return(false); #endif // WINDOWS_UWP }
/// <summary> /// Open Microphone /// </summary> private async void EnableMicrophone() { bool isMicAvailable = true; try { var mediaCapture = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio; await mediaCapture.InitializeAsync(settings); } catch (Exception) { isMicAvailable = false; } if (!isMicAvailable) { await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone")); } else { NotifyUser("Microphone was enabled", NotifyType.StatusMessage); } }
private async Task CameraHelper_OnNeedSetControls(Windows.Media.Capture.MediaCapture media) { //cpeMain.Width = ConfigHelper.Info.CameraPhotoSizeWidth; //cpeMain.Height = ConfigHelper.Info.CameraPhotoSizeHeight; await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { cpeMain.Source = media; }); }