簡體   English   中英

Windows Phone 8.1 MediaCapture會凍結手機

[英]Windows Phone 8.1 MediaCapture freezes the phone

我想制作一個簡單的應用程序,這將允許我檢查每個預覽框架的幾個參數,但我遇到了運行和停止預覽。

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        MediaCapture _MediaCapture;
        bool _recording;
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);


            var rearCamera = devices[0];
            if (devices.Count > 0)
            {

                rearCamera = devices.Single(currDev =>
                  currDev.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
                );
            }

            _MediaCapture = new MediaCapture();
            await _MediaCapture.InitializeAsync(new MediaCaptureInitializationSettings() { VideoDeviceId = rearCamera.Id });

// this is CaptureElement
            xCapture.Source = _MediaCapture;

            _recording = false;
        }

        protected override async void OnNavigatedFrom(NavigationEventArgs e)
        {
            if(_MediaCapture != null)
            {
                await _MediaCapture.StopPreviewAsync();
                await _MediaCapture.StopRecordAsync();

                _MediaCapture.Dispose();
                _MediaCapture = null;

                xCapture.Source = null;
            }


            base.OnNavigatedFrom(e);


        }

// button click handler
        private async void StartMeasure(object sender, RoutedEventArgs e)
        {
            if (_recording)
            {
                //await _MediaCapture.StopPreviewAsync();
                _MediaCapture.VideoDeviceController.TorchControl.Enabled = false;
                _recording = false;
            }
            else
            {
                //await _MediaCapture.StartPreviewAsync();
                _MediaCapture.VideoDeviceController.TorchControl.Enabled = true;
                _recording = true;
            }
        }
    }

在這種形式下,它完美地運作。

如果我取消注釋那些預覽行,它只能運行一次。

如果我按下按鈕三次:打開,關閉和再次打開我在啟用TorchControl時會出現異常。

System.Exception:來自HRESULT的異常:位於Pulsometr3.MainPage.d__d.MoveNext()的Windows.Media.Devices.TorchControl.put_Enabled(布爾值)的0xE801000D

HRESULT各不相同。

更奇怪的是,它有時會凍結手機(比如3次中的2次),我需要按住Power + Volume Down。

我嘗試使用[STAThread]裝飾所有方法,但它沒有幫助( http://technet.microsoft.com/en-ca/br226599 )。

更有趣的是,當我通過debbuger使用F10執行操作來跨越線路時,我可以根據需要多次切換預覽。 這很糟糕,因為調試器擁有所有線程,對嗎? 所以理論上沒有區別?

此外,手機有時會凍結部署...而這只是令人討厭。

有任何想法嗎?

我已經完全理解了......出於某種原因,微軟並不關心它是WP8的后續操作系統,這讓我非常難過。 但是也是半年前的夏天,我試過這個,也許你可以用谷歌搜索申請同意書,並仔細查看你的應用程序清單,如果你有前/后攝像頭和網絡攝像頭勾選:)如果它不起作用,那么運氣不好,你應該堅持使用wp 8.0版本,這在wp 8.1上的工作方式完全相同所以不要擔心:)其他libs如facebook東西或parse.com也行不通在wp 8.1 C#:)

我認為您的問題是啟用了頁面緩存。 嘗試在代碼中刪除此行this.NavigationCacheMode = NavigationCacheMode.Required;

如果我理解正確,按鈕有一個處理程序StartMeasure,這是一個異步方法,等待Start / StopPreviewAsync()。 問題可能是,如果您多次單擊該按鈕,可能仍在等待一個操作(正在進行中)而另一個操作也被調用,這可能會導致一些問題,因為它會嘗試以相同的方式啟動和停止預覽時間可能會導致一些競爭條件。 您可以通過添加鎖來管理對捕獲管理器的訪問以進行測試來檢查這一點。 在等待操作之后檢查bool並分配它肯定不是原子操作,因此也可能導致競爭條件。

    private object locker;

    private async void StartMeasure(object sender, RoutedEventArgs e)
    {
        lock (locker)
        {
            if (_recording)
            {
                await _MediaCapture.StopPreviewAsync();
            }
            else
            {
                await _MediaCapture.StartPreviewAsync();
            }
            _recording = !_recording;
            _MediaCapture.VideoDeviceController.TorchControl.Enabled = _recording;               
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM