locked
Starting camera on resuming windows store app

    Question

  • We are developing normal bar code scanner app for windows surface tablets. In onNavigatedTo method of page we are invoking camera..

    var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
            await App.camera.InitializeAsync(new MediaCaptureInitializationSettings
            {
                VideoDeviceId = devices[1].Id
            });
    
            cap.Source = App.camera;
            cap.Visibility = Visibility.Visible;
            await App.camera.StartPreviewAsync(); 

    It is working fine.

    Now App goes to background and comes back to foreground. But we are unable to invoke camera again on resume using same code

     await App.camera.StartPreviewAsync(); 

    How to do it?

    Wednesday, December 10, 2014 6:45 AM

Answers

  • Hi Vaibhav

    I would suggest you to dispose all the resource than only call StopPreviewAsync(), see my code:

            public MainPage()
            {
                this.InitializeComponent();
                Application.Current.Resuming += new EventHandler<Object>(App_Resuming);
            }
    
            private void App_Resuming(object sender, object e)
            {
                startCamera();
            }
    
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                startCamera();
            }
    
            async private void startCamera()
            {
                App.captureManager = new Windows.Media.Capture.MediaCapture();
                var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
                await App.captureManager.InitializeAsync(new MediaCaptureInitializationSettings
                {
                    VideoDeviceId = devices[1].Id
                });
    
                cap.Source = App.captureManager;
                cap.Visibility = Visibility.Visible;
                await App.captureManager.StartPreviewAsync(); 
            }

    and the suspending code:

            private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
    
                await App.captureManager.StopPreviewAsync();
                App.captureManager.Dispose();
                
                //TODO: Save application state and stop any background activity
                deferral.Complete();
            }

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 15, 2014 8:55 AM
    Moderator

All replies

  • Hi Vaibhav,

    What is your suspending code in your app.xaml.cs?

    Try to StopPreviewAsync and also Dispose while your app is about to suspend.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 11, 2014 2:56 AM
    Moderator
  • When app goes to background, we have called StopPreviewAsync() method and on resuming back we are calling StartPreviewAsync().
    Thursday, December 11, 2014 6:14 AM
  • Hi Vaibhav

    I would suggest you to dispose all the resource than only call StopPreviewAsync(), see my code:

            public MainPage()
            {
                this.InitializeComponent();
                Application.Current.Resuming += new EventHandler<Object>(App_Resuming);
            }
    
            private void App_Resuming(object sender, object e)
            {
                startCamera();
            }
    
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                startCamera();
            }
    
            async private void startCamera()
            {
                App.captureManager = new Windows.Media.Capture.MediaCapture();
                var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
                await App.captureManager.InitializeAsync(new MediaCaptureInitializationSettings
                {
                    VideoDeviceId = devices[1].Id
                });
    
                cap.Source = App.captureManager;
                cap.Visibility = Visibility.Visible;
                await App.captureManager.StartPreviewAsync(); 
            }

    and the suspending code:

            private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
    
                await App.captureManager.StopPreviewAsync();
                App.captureManager.Dispose();
                
                //TODO: Save application state and stop any background activity
                deferral.Complete();
            }

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 15, 2014 8:55 AM
    Moderator