locked
How to switch webcams during Preview

    Question

  • Hi,

             I was trying to develop a video preview application and I have two webcams connected to my system. I need to switch between these webcams while review is happening...

    Here in the following code :

    void MainPage::ReleasecurrentMediaCapture()
    {
     if (m_MediaCapture)
     {
      auto prevOp = m_MediaCapture->StopPreviewAsync();
      prevOp->Completed = ref new AsyncActionCompletedHandler([this](IAsyncAction ^resultOp)
      {
       m_MediaCapture = nullptr;
      });
      prevOp->Start();
     }
    }

    StopPreviewAsync() call is returning immediately and m_MediaCapture is not getting assigned with nullptr.

    Following is the mediacapture initialization and preview enabling code and it has started the video preview succesfully. Now when I select the second webcam from a combo box, the line "auto prevOp = _this->m_MediaCapture->StartPreviewAsync();" throws an exception. So I checked the ReleasecurrentMediaCapture( ) and found that the statement m_MediaCapture = nullptr; is not getting executed.

    void MainPage::InitCurrentMediaCapture()
    {
     
     ReleasecurrentMediaCapture();
     
     m_MediaCapture = ref new MediaCapture();
     
     auto initOp = m_MediaCapture->InitializeAsync(m_CaptureSettings);
     initOp->Completed = ref new AsyncActionCompletedHandler([this](IAsyncAction ^findOp)
     {
      this->PreviewMedia->Source = this->m_MediaCapture;

      auto prevOp = _this->m_MediaCapture->StartPreviewAsync();
      prevOp->Completed = ref new AsyncActionCompletedHandler([this](IAsyncAction ^resultOp)
      {
       //success
      });


     prevOp->Start();
     });
     initOp->Start();
    }

    Any idea about this problem?

    -PRADEEP

    Friday, February 24, 2012 6:02 AM

Answers

  • Hello Cristianohh,

    Here is some untested C# code that should do the trick. If this doesn't work for you let me know.

            private async void StartCapture(MediaCapture mc, int iIdx)
            {
                if (_DeviceList.Count == 0 || iIdx > _DeviceList.Count)
                {
                    throw new Exception("Error: No video capture devices");
                }
    
                _eMCState = EMCState.running;
    
                _CaptureSettings.AudioDeviceId = "";
                _CaptureSettings.VideoDeviceId = _DeviceList[iIdx].Id;
                _CaptureSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
                _CaptureSettings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
                _CaptureSettings.RealTimeModeEnabled = true;
    
                await mc.InitializeAsync(_CaptureSettings);
    
                _CE.Source = mc;
    
                await mc.StartPreviewAsync();
            }
    
            private async void  _btnStart_Click_1(object sender, RoutedEventArgs e)
            {
                var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    
                if (devices.Count > 0)
                {
                    for (int i = 0; i < devices.Count; i++)
                    {
                        _DeviceList.Add(devices[i]);
                    }
                }
    
                StartCapture(_MC, 0);
            }
    
            private async void _btnSwitch_Click_1(object sender, RoutedEventArgs e)
            {
                if (_eMCState == EMCState.running)
                {
                    await _MC.StopPreviewAsync();
    
                    _eMCState = EMCState.stopped;
                }
    
                _MC = null;
    
                _MC = new MediaCapture();
    
                _iIdx = _iIdx ^ 1;
    
                StartCapture(_MC, 1);
            }


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

    • Marked as answer by Bob_Bao Wednesday, March 07, 2012 5:42 AM
    Friday, March 02, 2012 11:47 PM
    Moderator

All replies

  • Hi,

    I got a solution for this problem and now I can switch the webcams during preview itself.

    if anyone is interested in this topic please let me know.

    -Pradeep

    Friday, February 24, 2012 9:35 AM
  • It would be helpful for the community if you could post some general approach you used to accomplish your task.

    Thanks,

    -David

    Wednesday, February 29, 2012 4:10 PM
    Moderator
  • I am interested, can you post the solution here?
    Wednesday, February 29, 2012 9:36 PM
  • Hello Cristianohh,

    Here is some untested C# code that should do the trick. If this doesn't work for you let me know.

            private async void StartCapture(MediaCapture mc, int iIdx)
            {
                if (_DeviceList.Count == 0 || iIdx > _DeviceList.Count)
                {
                    throw new Exception("Error: No video capture devices");
                }
    
                _eMCState = EMCState.running;
    
                _CaptureSettings.AudioDeviceId = "";
                _CaptureSettings.VideoDeviceId = _DeviceList[iIdx].Id;
                _CaptureSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
                _CaptureSettings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
                _CaptureSettings.RealTimeModeEnabled = true;
    
                await mc.InitializeAsync(_CaptureSettings);
    
                _CE.Source = mc;
    
                await mc.StartPreviewAsync();
            }
    
            private async void  _btnStart_Click_1(object sender, RoutedEventArgs e)
            {
                var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    
                if (devices.Count > 0)
                {
                    for (int i = 0; i < devices.Count; i++)
                    {
                        _DeviceList.Add(devices[i]);
                    }
                }
    
                StartCapture(_MC, 0);
            }
    
            private async void _btnSwitch_Click_1(object sender, RoutedEventArgs e)
            {
                if (_eMCState == EMCState.running)
                {
                    await _MC.StopPreviewAsync();
    
                    _eMCState = EMCState.stopped;
                }
    
                _MC = null;
    
                _MC = new MediaCapture();
    
                _iIdx = _iIdx ^ 1;
    
                StartCapture(_MC, 1);
            }


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

    • Marked as answer by Bob_Bao Wednesday, March 07, 2012 5:42 AM
    Friday, March 02, 2012 11:47 PM
    Moderator
  • Hi Pradeep,

    I know this topic is rather old, but I encountered a similar problem. I am very interested in knowing how you managed to solve this issue, so if you could post it here that would be great.

    Thanks in advance!

    C

    Thursday, December 19, 2013 8:27 AM