locked
camera preview stops when user switch app and come back within 10 sec

    Question

  • I have a camera app which based on sample from msdn. I am facing problem when user switch away from camera app and return back   immediately it stops the preview operation.If I move away from camera app for more than 10 sec and then came back preview works fine.

    ///code:

    void CameraApp::MainPage::OnSuspending(Platform::Object^ sender,Windows::ApplicationModel::SuspendingEventArgs^ e)
    {
    log.OutputLog(L"MainPage::OnSuspending");

    ReleaseMediaCapture();
    int a =0;
    }
    void CameraApp::MainPage::OnResuming(Platform::Object^ sender,Platform::Object^ e)
    {
    log.OutputLog(L"MainPage::OnResuming");
    InitMediaCapture();
    int a =0;
    }

    void CameraApp::MainPage::ReleaseMediaCapture()
    {
    log.OutputLog(L"MainPage::ReleaseMediaCapture");
    if (m_MediaCaptureMgr )
    {
    task<void> releaseMediaCapture(m_MediaCaptureMgr->StopPreviewAsync());
    auto _this = this;
    releaseMediaCapture.then([_this]
    {
    delete _this->m_MediaCaptureMgr;
    _this->m_MediaCaptureMgr = nullptr;
    _this->log.OutputLog(L"MainPage::StopPreviewAsync");
    });
    }
    }

    void CameraApp::MainPage::InitMediaCapture()
    {
    log.OutputLog(L"MainPage::InitMediaCapture");
    if (m_MediaCaptureMgr )
    {
    task<void> releaseMediaCapture(m_MediaCaptureMgr->StopPreviewAsync());
    releaseMediaCapture.then([=]
    {
    log.OutputLog(L"MainPage::InitMediaCapture---->InitializeAsync()");
    m_MediaCaptureMgr = nullptr;
    auto _this = this;
    m_MediaCaptureMgr = ref new MediaCapture();

    task<void> stratPreview(m_MediaCaptureMgr->InitializeAsync(m_CaptureInitSettings));
    stratPreview.then([_this]
    {

    _this->previewElement->Source = _this->m_MediaCaptureMgr;
    task<void> startPrev(_this->m_MediaCaptureMgr->StartPreviewAsync());
    _this->log.OutputLog(L"MainPage::InitMediaCapture---->InitializeAsync()");
    startPrev.then([=]
    {
    _this->log.OutputLog(L"MainPage::InitMediaCapture---->StartPreviewAsync()");
    return _this->GetCameraSettings();
    });
    });
    });
    }
    else
    {
    auto _this = this;
    m_MediaCaptureMgr = ref new MediaCapture();

    task<void> stratPreview(m_MediaCaptureMgr->InitializeAsync(m_CaptureInitSettings));
    stratPreview.then([_this]
    {
    _this->log.OutputLog(L"MainPage::InitializeAsync");
    _this->previewElement->Source = _this->m_MediaCaptureMgr;
    task<void> startPrev(_this->m_MediaCaptureMgr->StartPreviewAsync());
    startPrev.then([=]
    {
    _this->log.OutputLog(L"MainPage::StartPreviewAsync");
    return _this->GetCameraSettings();
    });
    });

    }
    }

    after going through the App Life Cycle I came to know that App suspends after 10 sec when user switch away from the  app. OnSuspending() gets called only after 10 sec.So if someone immediately switch App away and come back it dont call OnSuspending().So if it deosnot suspend it wont call OnResume() also.

    According msdn App remain alive till 10 sec and than it goes to suspend mode.If that is the case camera preview should work fine.I checked debugview output as well  and that also confirm my understanding.

    I dont know what is going wrong here.Please help me on this.



    • Edited by chris_vr Wednesday, September 26, 2012 1:17 PM
    Tuesday, September 25, 2012 6:13 PM

All replies

  • Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 27, 2012 3:00 AM
  • When your app is not active, the connectio to your device will be lost immediately. I belive this is an original design in Win8 OS.

    You can monitor the event of 'SoundLevelChanged'.

    Thursday, September 27, 2012 6:23 AM
  • When your app is not active, the connectio to your device will be lost immediately. I belive this is an original design in Win8 OS.

    You can monitor the event of 'SoundLevelChanged'.

         SoundLevelChanged Event raised when the sound level changes.I dont understand what is the meaning of Sound level chages. I have regd this event.It Supposed to gets fired when sound level change, but I am getting this event whenever I switch away from the App.I wanted something which will be fired when I Switch back to the App.
    Thursday, September 27, 2012 11:19 AM
  • Hello,

    Vtshe is correct. When your video capture app no longer has focus the D3D surface that is used to render the video is lost. The surface is not automatically recreated. You need to managed this scenario in your application. Unfortunately we do not have an elegant way to handle this scenario as of yet. The current recommendation is to register the "SoundLevelChanged" event. This event will be called with your app looses focus and should also be called when you app regains focus. Please take a look at the MediaCapture sample for detail on how to implement this functionality in your application.

    Media capture using webcam sample

    http://code.msdn.microsoft.com/windowsapps/Media-Capture-Sample-adf87622

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, September 28, 2012 1:23 AM
    Moderator
  • Hello,

    Vtshe is correct. When your video capture app no longer has focus the D3D surface that is used to render the video is lost. The surface is not automatically recreated. You need to managed this scenario in your application. Unfortunately we do not have an elegant way to handle this scenario as of yet. The current recommendation is to register the "SoundLevelChanged" event. This event will be called with your app looses focus and should also be called when you app regains focus. Please take a look at the MediaCapture sample for detail on how to implement this functionality in your application.

    Media capture using webcam sample

    http://code.msdn.microsoft.com/windowsapps/Media-Capture-Sample-adf87622

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

       I found one more event VisibilityChanged in App life cycle and I used it and  is working fine.

       Advantage is,it has got VisibilityChangedEventArgs^ e which will give you Window is visible or not based on this one can take course of      action.whereas SoundLevelChanged doesnot give visibility value instead one need to check Windows::Media::MediaControl::SoundLevel

     Please confirm whether this is a right approch to do this???

     

    m_eventRegistrationToken = Window::Current->VisibilityChanged += ref new            Windows::UI::Xaml::WindowVisibilityChangedEventHandler(this,&MainPage::OnVisibilityChanged);

      void CameraApp::MainPage::OnVisibilityChanged(Object^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ e)
       {
               if (e->Visible)
                     // do something 
              else
                    // do something

       }



    • Edited by chris_vr Friday, September 28, 2012 10:47 AM
    Friday, September 28, 2012 10:46 AM
  • The SoundLevelChanged while not idea did seem to work for me with respect to suspending and resuming the vidieo preview.  Here is my code:

    public MapPage() {
        this.InitializeComponent();
    
        MediaControl.SoundLevelChanged += async (s, e) => {
            await this.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                async () => {
                    if (MediaControl.SoundLevel == SoundLevel.Muted) {
                        if (this._mediaCapture != null) {
                            await this._mediaCapture.StopPreviewAsync();
                        }
                    }
                    else {
                        if (this.ToggleButtonShowVideo.IsChecked.Value) {
                            if (this._mediaCapture != null) {
                                await this._mediaCapture.StartPreviewAsync();
                            }
                        }
                    }
                }
            );
        };
    }


    Tuesday, September 24, 2013 10:47 PM