2016-04-15 65 views
0

如何以編程方式禁用相機快門聲,當我拍照時總是發出聲音,我正在調用發出聲音的MediaCapture的Dispose方法。有什麼方法可以禁用該聲音嗎?如何在不播放快門聲的情況下丟棄MediaCapture

下面是初始化代碼:

private async Task InitializeQrCode(CaptureElement captureElement) 
    { 
     string error = null; 
     try 
     { 
      if (mediaCapture == null) 
      { 
       mediaCapture = new MediaCapture(); 

       var _deviceInformation = await GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel.Back); 

       var settings = new MediaCaptureInitializationSettings(); 
       settings.StreamingCaptureMode = StreamingCaptureMode.Video; 
       settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview; 
       settings.AudioDeviceId = ""; 
       if (_deviceInformation != null) 
        settings.VideoDeviceId = _deviceInformation.Id; 

       await mediaCapture.InitializeAsync(settings); 

       var maxResolution = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2); 
       await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxResolution); 

       if (mediaCapture.VideoDeviceController.FocusControl.FocusChangedSupported) 
       { 
        var focusSettings = new FocusSettings(); 
        focusSettings.AutoFocusRange = AutoFocusRange.FullRange; 
        focusSettings.Mode = FocusMode.Auto; 
        focusSettings.WaitForFocus = true; 
        focusSettings.DisableDriverFallback = false; 

        mediaCapture.VideoDeviceController.FocusControl.Configure(focusSettings); 
       } 

       await mediaCapture.VideoDeviceController.ExposureControl.SetAutoAsync(true); 

       mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees); 
       mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees); 
      } 

      captureElement.Source = mediaCapture; 
      await mediaCapture.StartPreviewAsync(); 
     } 
     catch (Exception ex) 
     { 
      DialogBox.ShowOkMessage(this, "Error:" + ex.Message); 
     } 
    } 

而這裏的是越來越效力於捕捉圖像,在CapturePhotoToStreamAsync快門聲代碼:

var stream = new InMemoryRandomAccessStream(); 
      await mediaCapture.CapturePhotoToStreamAsync(imgProp, stream); 


      stream.Seek(0); 
      var wbm = new WriteableBitmap(WPAppConstants.Dimension.ImageEncodingWidth, WPAppConstants.Dimension.ImageEncodingHeight); 
      await wbm.SetSourceAsync(stream); 
      var result = bcReader.Decode(wbm); 


      if (result != null) 
      { 
       var torch = mediaCapture.VideoDeviceController.TorchControl; 
       if (torch.Supported) torch.Enabled = false; 
       await StopQrCodeScan(); 

       var resultEvent = Result; 
       if (resultEvent != null) 
       { 
        resultEvent(null, new CameraClickedEventArgs { EncodedData = result.Text }); 
       } 
      } 

演示項目Onedrive Link

+0

調用在MediaCapture上Dispose不會在正常情況下觸發快門聲。您應該發佈可能顯示這些症狀的最小代碼,並且我們將能夠幫助您更好。 – Mike

回答

0

嘗試mediaCaptureElement.Source = null;然後await capture.StopPreviewAsync();然後capture.Dispose();

+0

試過,但仍然播放快門聲。 –

+0

嘗試禁用系統設置中可用的快門聲音。有樣品打開設置頁https://code.msdn.microsoft。com/windowsapps/how-to-Launch-settings-6d68c5ba – Elektryczny

+0

謝謝,但商店裏很少有應用程序在快門聲音打開的情況下不會播放快門聲音事件,如#Scan和Comment。 –

1

的問題,你提出的是:

如何處置MediaCapture不播放快門聲音

這個問題的答案是調用(假設你正在運行的預覽,因爲你應該幾乎總是在捕捉時運行它):

await mediaCapture.StopPreviewAsync(); 
captureElement.Source = null; 
mediaCapture.Dispose(); 

執行該代碼不會播放快門聲。作爲參考,請參閱UWP CameraStarterKit SDK sample,它只會在捕捉開始或停止時播放快門聲音。

話雖這麼說,我注意到你的問題,你說的一些矛盾:

它總是使聲音時我拍照,我調用Dispose MediaCapture的 方法,這使得聲音

暗示捕獲和處置都觸發聲音。你可能會讓這些概念有點困惑。 Dispose method of the MediaCapture釋放非託管資源。 Calling Dispose本身不會觸發快門聲音,它與拍攝或拍攝視頻時可能聽到的快門聲音無關。當您撥打CapturePhotoToStorageFileAsyncStartRecordToStorageFileAsync等方法通知用戶(及其周圍的人)相機處於活動狀態時,聲音播放。

我不知道如何爲您的應用專門禁用快門聲。有沒有你想要做的事情需要無聲捕捉?

PS:這有點偏離主題,但是您已經發布了比重現快門聲音所需的代碼多得多的代碼,其中包括一些看起來輪換的代碼。我想向您推薦介紹detecting screen/device rotation and setting the preview rotation的MSDN頁面。再往下,你還會學到推薦的方法rotate a captured video

+0

感謝您的詳細信息,我盡我所能,但仍然有問題,請檢查我編輯的帖子我上傳演示項目爲您參考,在該項目中,當您捕獲任何圖像,並嘗試處理資源時,它會產生快門聲 –

+0

上傳樣品的任何幫助嗎? –

+0

你在運行什麼設備,以及安裝了哪個版本的操作系統?它的工作原理與我在Windows 10 Lumia 950上描述的一樣,快門聲音只在點擊Capture按鈕時播放,而不是在Dispose按鈕上播放。 – Mike

相關問題