简体   繁体   中英

Windows Phone 8.1 MediaCapture freezes the phone

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?

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# :)

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

if I understand correctly the button has a handler StartMeasure which is an async method and awaits for Start/StopPreviewAsync(). The problem might be that if you click the button more than once the one action might be still awaited(in progress) and the other one is also called, this might cause some issues because it will try to start and stop the preview at the same time which will probably lead to some race conditions. You could check this by adding a lock to manage the access to the capture manager in order to test this. Also checking the bool and assigning it after an awaited operation is for sure not an atomic operation so that could lead to race conditions too.

    private object locker;

    private async void StartMeasure(object sender, RoutedEventArgs e)
    {
        lock (locker)
        {
            if (_recording)
            {
                await _MediaCapture.StopPreviewAsync();
            }
            else
            {
                await _MediaCapture.StartPreviewAsync();
            }
            _recording = !_recording;
            _MediaCapture.VideoDeviceController.TorchControl.Enabled = _recording;               
        }
    }

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