如何以编程方式禁用相机快门声,当我拍照时总是发出声音,我正在调用MediaCapture的Dispose方法来发出声音。有没有办法禁用那个声音?
以下是初始化代码:
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 :(得分:1)
你提出的问题是:
如何在不播放快门声的情况下处理MediaCapture
答案就是打电话(假设你正在运行预览,因为你在拍摄时几乎总是会运行它):
await mediaCapture.StopPreviewAsync();
captureElement.Source = null;
mediaCapture.Dispose();
执行该代码不会播放快门声。作为参考,请参阅UWP CameraStarterKit SDK sample,它仅在捕捉开始或停止时播放快门声。
话虽如此,我注意到你的问题中存在一些矛盾,你说:
拍照时总是发出声音,我叫Dispose 发出声音的MediaCapture的方法
暗示捕获和处置都会触发声音。您可能会对这些概念感到有些困惑。 Dispose method of the MediaCapture释放非托管资源。调用Dispose不会自动触发快门声音,它与您拍摄照片或捕捉视频时可能听到的快门声音无关。当您调用CapturePhotoToStorageFileAsync或StartRecordToStorageFileAsync等方法来通知用户(及其周围的人)相机处于活动状态时,会播放声音。
我不知道如何为您的应用专门禁用快门声音。你有什么想做的事需要静音捕捉吗?
PS:这有点偏离主题,但是您发布了比重现快门声音所需的代码更多的代码,包括一些看似旋转的代码。我想推荐您访问有关detecting screen/device rotation and setting the preview rotation的MSDN页面。再往下,您还将学习rotate a captured video的推荐方法。
答案 1 :(得分:0)
尝试mediaCaptureElement.Source = null;
然后await capture.StopPreviewAsync();
然后capture.Dispose();