简体   繁体   中英

UWP Media Capture Not Recording Video through WebCam

I am trying to develop a UWP app where I need to record video via webcam. I have followed microsoft tutorial about it here but the problem is I am getting following error again and again and recording does not work.

Error

"This object needs to be initialized before the requested operation can be carried out.\\r\\nThis object needs to be initialized before the requested operation can be carried out."

Here is my code:

int counter = 1;
                var myVideos = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Videos);
                StorageFile file = await myVideos.SaveFolder.CreateFileAsync("survillance "+DateTime.Now.ToString("dd-MM-yyyy")+"_"+counter+".wmv", CreationCollisionOption.GenerateUniqueName);

                try
                {
                    MediaEncodingProfile recordProfile = null;
                    recordProfile = MediaEncodingProfile.CreateWmv(VideoEncodingQuality.Auto);

                    _mediaRecording = await _mediaCapture.PrepareLowLagRecordToStorageFileAsync(recordProfile,file);
                    await _mediaRecording.StartAsync();


                    status.Text = "Video recording in progress...";


                }
                catch (Exception ex)
                {

                    status.Text = "Failed to Capture...";
                    var msg = new MessageDialog(ex.Message, "Capturing Error").ShowAsync();
                }

Please help me in figuring out the problem. Thanks

You forgot to call MediaCapture.InitializeAsync() before starting the capture.

MediaEncodingProfile recordProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);

_mediaCapture   = new MediaCapture();
_mediaCapture.InitializeAsync();

_mediaRecording = await _mediaCapture.PrepareLowLagRecordToStorageFileAsync(recordProfile,file);
await _mediaRecording.StartAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM