0

I am making an Flashlight app, in which I need to use the camera's LED constantly on pressing ON button and OFF it on pressing the same button. I followed this article Turning on the LED with the video camera using Reflection. The ON/OFF operation works fine only once. The Code is as:

 private VideoCamera _videoCamera;
 private VideoCameraVisualizer _videoCameraVisualizer;
 bool _isFlashOff = true;

 private void FlashButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (_isFlashOff)
            {
                _isFlashOff = false;

                // Check to see if the camera is available on the device.
                if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
                {
                   // Use standard camera on back of device.
                    _videoCamera = new VideoCamera();
                   // Event is fired when the video camera object has been initialized.
                    _videoCamera.Initialized += VideoCamera_Initialized;

                    // Add the photo camera to the video source
                    _videoCameraVisualizer = new VideoCameraVisualizer();
                    _videoCameraVisualizer.SetSource(_videoCamera);
                }
            }
            else
            {                    
                _isFlashOff = true;
                _videoCamera.StopRecording();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void VideoCamera_Initialized(object sender, EventArgs e)
    {
        _videoCamera.LampEnabled = true;
        _videoCamera.StartRecording();
    }

Since there was no implementation of StopRecording Method in VideoCamera class as specified in the article: Turning on the LED with the video camera using Reflection . I made the function as:

 public void StopRecording()
    {
        // Invoke the stop recording method on the video camera object.
         _videoCameraStopRecordingMethod.Invoke(_videoCamera, null);
    }

The problem is when I again press ON button "Exception' is thrown as "TargetInvocationException". I am unable to figure out the problem that causes exception. Is StopRecording() function right..??

Shaan_14
  • 51
  • 1
  • 10
  • How are you instanciating `_videoCameraStopRecordingMethod` ? – Kevin Gosse Aug 28 '12 at 06:07
  • I have declared it as : private MethodInfo _videoCameraStopRecordingMethod and Instantiated as _videoCameraStopRecordingMethod =videoCameraType.GetMethod("StopRecording"); is anything wrong with the public void StopRecording() method...?? – Shaan_14 Aug 28 '12 at 10:45
  • Can anybody help me with this..?? How I need to create StopRecording() method...?? – Shaan_14 Aug 30 '12 at 06:54

2 Answers2

2

That's because you should initialize the camera only once. Do it during the OnNavigatedTo event, then re-use the same instances:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {                    
        // Use standard camera on back of device.
        _videoCamera = new VideoCamera();

        // Event is fired when the video camera object has been initialized.
        _videoCamera.Initialized += VideoCamera_Initialized;


        // Add the photo camera to the video source
        _videoCameraVisualizer = new VideoCameraVisualizer();
        _videoCameraVisualizer.SetSource(_videoCamera);
    }

    private void VideoCamera_Initialized(object sender, EventArgs e)
    {
        isInitialized = true;
    }

    bool isInitialized;
    bool isFlashEnabled;

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        if (!isInitialized)
        {
            MessageBox.Show("Please wait during camera initialization");
            return;
        }

        if (!isFlashEnabled)
        {
            isFlashEnabled = true;

            // Check to see if the camera is available on the device.
            if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
            {
                _videoCamera.LampEnabled = true;
                _videoCamera.StartRecording();
            }
        }
        else
        {
            isFlashEnabled = false;

            _videoCamera.StopRecording();
        }
    }
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • Awesum reply.... Thanks a lot..!!!! KooKiz...You saved my day...Realy thanks for the reply... Just a question again how can I can achieve the blink functionality to this flashlight..?? – Shaan_14 Sep 02 '12 at 12:48
  • @Shaan_14 I guess, put a timer and alternatively turn on and off the flashlight when it ticks – Kevin Gosse Sep 02 '12 at 13:25
  • Just a issue with the code when I press Back button to move to MainPage and again navigate to Flashlight Page I get the "TargetInvocationException" exception in line _videoCameraVisualizer.SetSource(_videoCamera); of OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) method. Whereas If I press Windows Button or search button it starts working.. – Shaan_14 Sep 02 '12 at 18:35
0

Try this:

http://msdn.microsoft.com/en-us/library/hh202949.aspx

Gambit
  • 2,455
  • 18
  • 20