'System.UnauthorizedAccessException'无效的跨线程访问。在创建CaptureSource时 [英] 'System.UnauthorizedAccessException' Invalid cross-thread access. when creating CaptureSource

查看:71
本文介绍了'System.UnauthorizedAccessException'无效的跨线程访问。在创建CaptureSource时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试录制视频:
https://msdn.microsoft.com/en-us/library/windows/apps/hh394041%28v=vs.105%29.aspx

I am trying to record a video following this: https://msdn.microsoft.com/en-us/library/windows/apps/hh394041%28v=vs.105%29.aspx

但是当我初始化一个CaptureSource(InitializeVideoRecorder())时,我在System.Windows中得到这个

but when I initialize a CaptureSource (InitializeVideoRecorder()) I am getting this

第一次机会di tipo'System.UnauthorizedAccessException' .ni.dll无效的跨线程访问。

first-chance di tipo 'System.UnauthorizedAccessException' in System.Windows.ni.dll Invalid cross-thread access.

我的代码有什么问题?  CAMERA和  MICROPHONE被设置为权限) 

what is wrong with my code? CAMERA and MICROPHONE are set up as permissions) 

 public void InitializeVideoRecorder()
        {
            try {
                if (captureSource == null)
                {
                    // Create the VideoRecorder objects.
                    captureSource = new CaptureSource();
                    fileSink = new FileSink();

                    videoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

                    // Add eventhandlers for captureSource.
                    //   captureSource.CaptureFailed += new EventHandler<ExceptionRoutedEventArgs>(OnCaptureFailed);

                    // Initialize the camera if it exists on the phone.
                    if (videoCaptureDevice != null)
                    {
                        // Create the VideoBrush for the viewfinder.
                        videoRecorderBrush = new VideoBrush();
                        videoRecorderBrush.SetSource(captureSource);

                        // Display the viewfinder image on the rectangle.
                        //  viewfinderRectangle.Fill = videoRecorderBrush;

                        // Start video capture and display it on the viewfinder.
                        captureSource.Start();

                        // Set the button state and the message.
                        //  UpdateUI(ButtonState.Initialized, "Tap record to start recording...");
                    }
                    else
                    {
                        // Disable buttons when the camera is not supported by the phone.
                        //  UpdateUI(ButtonState.CameraNotSupported, "A camera is not supported on this phone.");
                    }
                }
            }
            catch (Exception ex) {
                Debug.WriteLine("ex: " + ex.Message);
            }

            
        }


        // Set recording state: start recording.
        private void StartVideoRecording()
        {
            try
            {
                // Connect fileSink to captureSource.
                if (captureSource.VideoCaptureDevice != null
                    && captureSource.State == CaptureState.Started)
                {
                    captureSource.Stop();

                    // Connect the input and output of fileSink.
                    fileSink.CaptureSource = captureSource;
// fileSink.IsolatedStorageFileName = isoVideoFileName;
                }

                // Begin recording.
                if (captureSource.VideoCaptureDevice != null
                    && captureSource.State == CaptureState.Stopped)
                {
                    captureSource.Start();
                }

                // Set the button states and the message.
              //  UpdateUI(ButtonState.Recording, "Recording...");
            }

            // If recording fails, display an error.
            catch (Exception e)
            {
                //this.Dispatcher.BeginInvoke(delegate()
                //{
                //    txtDebug.Text = "ERROR: " + e.Message.ToString();
                //});
            }
        }

        // Set the recording state: stop recording.
        private void StopVideoRecording()
        {
            try
            {
                // Stop recording.
                if (captureSource.VideoCaptureDevice != null
                && captureSource.State == CaptureState.Started)
                {
                    captureSource.Stop();

                    // Disconnect fileSink.
                    fileSink.CaptureSource = null;
                    fileSink.IsolatedStorageFileName = null;

                    // Set the button states and the message.
                   // UpdateUI(ButtonState.NoChange, "Preparing viewfinder...");

                    StartVideoPreview();
                }
            }
            // If stop fails, display an error.
            catch (Exception e)
            {
                //this.Dispatcher.BeginInvoke(delegate()
                //{
                //    txtDebug.Text = "ERROR: " + e.Message.ToString();
                //});
            }
        }

        // Set the recording state: display the video on the viewfinder.
        private void StartVideoPreview()
        {
            try
            {
                // Display the video on the viewfinder.
                if (captureSource.VideoCaptureDevice != null
                && captureSource.State == CaptureState.Stopped)
                {
                    // Add captureSource to videoBrush.
                    videoRecorderBrush.SetSource(captureSource);

                    // Add videoBrush to the visual tree.
                   // viewfinderRectangle.Fill = videoRecorderBrush;

                    captureSource.Start();

                    // Set the button states and the message.
                    // UpdateUI(ButtonState.Ready, "Ready to record.");
                }
            }
            // If preview fails, display an error.
            catch (Exception e)
            {
                //this.Dispatcher.BeginInvoke(delegate()
                //{
                //    txtDebug.Text = "ERROR: " + e.Message.ToString();
                //});
            }
        }


        private void OnCaptureFailed(object sender, ExceptionRoutedEventArgs e)
        {
            //this.Dispatcher.BeginInvoke(delegate()
            //{
            //    txtDebug.Text = "ERROR: " + e.ErrorException.Message.ToString();
            //});
        }

        // Display the viewfinder when playback ends.
        public void VideoPlayerMediaEnded(object sender, RoutedEventArgs e)
        {
            // Remove the playback objects.
          //  DisposeVideoPlayer();

            StartVideoPreview();
        }




s

推荐答案

您好,

可能你没有在UI线程的上下文中初始化CaptureSource。在后台或工作线程上初始化它可能会导致此错误。确保你实例化对象并在UI线程上初始化它。

Likely you are not initializing the CaptureSource in the context of the UI thread. Initializing it on a background or worker thread may cause this error. Make sure you instantiate the object and initialize it on the UI thread.

我希望这有帮助,

James


这篇关于'System.UnauthorizedAccessException'无效的跨线程访问。在创建CaptureSource时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆