Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone 8.1 MediaCapture freezes the phone

Tags:

I want to make a simple app that will allow me to check few parameters of every frame of preview, but I got stuck at running and stopping preview.

    /// <summary>     /// An empty page that can be used on its own or navigated to within a Frame.     /// </summary>     public sealed partial class MainPage : Page     {         MediaCapture _MediaCapture;         bool _recording;         public MainPage()         {             this.InitializeComponent();              this.NavigationCacheMode = NavigationCacheMode.Required;         }          /// <summary>         /// Invoked when this page is about to be displayed in a Frame.         /// </summary>         /// <param name="e">Event data that describes how this page was reached.         /// This parameter is typically used to configure the page.</param>         protected override async void OnNavigatedTo(NavigationEventArgs e)         {             var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);               var rearCamera = devices[0];             if (devices.Count > 0)             {                  rearCamera = devices.Single(currDev =>                   currDev.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back                 );             }              _MediaCapture = new MediaCapture();             await _MediaCapture.InitializeAsync(new MediaCaptureInitializationSettings() { VideoDeviceId = rearCamera.Id });  // this is CaptureElement             xCapture.Source = _MediaCapture;              _recording = false;         }          protected override async void OnNavigatedFrom(NavigationEventArgs e)         {             if(_MediaCapture != null)             {                 await _MediaCapture.StopPreviewAsync();                 await _MediaCapture.StopRecordAsync();                  _MediaCapture.Dispose();                 _MediaCapture = null;                  xCapture.Source = null;             }               base.OnNavigatedFrom(e);           }  // button click handler         private async void StartMeasure(object sender, RoutedEventArgs e)         {             if (_recording)             {                 //await _MediaCapture.StopPreviewAsync();                 _MediaCapture.VideoDeviceController.TorchControl.Enabled = false;                 _recording = false;             }             else             {                 //await _MediaCapture.StartPreviewAsync();                 _MediaCapture.VideoDeviceController.TorchControl.Enabled = true;                 _recording = true;             }         }     } 

In this form it works perfectly.

If I uncomment those preview lines it works, but only once.

If I press the button three times: on, off and on again I get exception at line with enabling TorchControl.

System.Exception: Exception from HRESULT: 0xE801000D at Windows.Media.Devices.TorchControl.put_Enabled(Boolean value) at Pulsometr3.MainPage.d__d.MoveNext()

The HRESULT varies.

Whats even more weird, it sometimes freezes the phone (like 2 out of 3 times) and I need to hold Power + Volume Down.

I tried decorating all methods with [STAThread], but it didn't help (http://technet.microsoft.com/en-ca/br226599).

What's even more more interesting, when I hold operations by debbuger using F10 to step over lines I am able to toggle preview as many times as I possibly want. It's werid, since debugger hold all threads, right? So in theory there is no difference?

Also, phone sometimes freezes on deploy... And that's just annoying.

Any ideas?

like image 500
Krzysztof Skowronek Avatar asked Aug 26 '14 20:08

Krzysztof Skowronek


2 Answers

I've got exactly into this...for some reason microsoft does not care much for it's successor OS to WP8, which makes me really sad. But it was also a half year ago during summer, I've tried this, maybe you can give a shot to googling on application consents and also double check your app manifests, if you have front/rear camera and webcam ticked in :) Besides that if it won't work, then bad luck, you are ought to stick with wp 8.0 version, which works exactly the same on wp 8.1 so do not worry :) also other libs like facebook stuff or parse.com won't work on wp 8.1 C# :)

like image 92
Citrus Avatar answered Oct 21 '22 19:10

Citrus


I think your problem is the page cache enabled. Try to remove this line in your code this.NavigationCacheMode = NavigationCacheMode.Required;

like image 39
user6174178 Avatar answered Oct 21 '22 18:10

user6174178