【问题标题】:Video recorder app in Windows Phone 8.1Windows Phone 8.1 中的录像机应用程序
【发布时间】:2015-04-13 05:23:44
【问题描述】:

我要在 Windows Phone 8.1 (RT) 中编写一个Video Recorder 应用程序

我使用了 Windows 参考中提供的示例

 public sealed partial class CamcorderMainPage : Page
    {
        StatusBar StatusBarObject = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
        string StatusBarHeader = "Firefly Moments";
        MediaCaptureInitializationSettings _captureInitSettings;
        List<Windows.Devices.Enumeration.DeviceInformation> _deviceList;
        Windows.Media.MediaProperties.MediaEncodingProfile _profile;
        Windows.Media.Capture.MediaCapture _mediaCapture;
        bool _recording = false;
        bool _previewing = false;
        string NoCameraError = "No camera device is found ";

        public CamcorderMainPage()
        {
            this.InitializeComponent();
            this.Loaded += CamcorderMainPage_Loaded;
        }

        void CamcorderMainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();
            EnumerateCameras();
        }


        private async void EnumerateCameras()
        {
            var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(
                Windows.Devices.Enumeration.DeviceClass.VideoCapture);

            _deviceList = new List<Windows.Devices.Enumeration.DeviceInformation>();

            // Add the devices to deviceList
            if (devices.Count > 0)
            {

                for (var i = 0; i < devices.Count; i++)
                {
                    _deviceList.Add(devices[i]);
                }

                InitCaptureSettings();
                InitMediaCapture();
               // rootPage.NotifyUser("Initialization complete.", NotifyType.StatusMessage);

            }
            else
            {
                StatusBarObject.ProgressIndicator.Text = NoCameraError;
                //rootPage.NotifyUser("No camera device is found ", NotifyType.ErrorMessage);
            }
        }


        private void InitCaptureSettings()
        {
            _captureInitSettings = null;
            _captureInitSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
            _captureInitSettings.AudioDeviceId = "";
            _captureInitSettings.VideoDeviceId = "";
            _captureInitSettings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.AudioAndVideo;
            _captureInitSettings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview;

            if (_deviceList.Count > 0)
                _captureInitSettings.VideoDeviceId = _deviceList[0].Id;
        }

        // Create a profile.
        private void CreateProfile()
        {
            _profile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateMp4(
            Windows.Media.MediaProperties.VideoEncodingQuality.Qvga);
        }

        // Create and initialze the MediaCapture object.
        public async void InitMediaCapture()
        {
            _mediaCapture = null;
            _mediaCapture = new Windows.Media.Capture.MediaCapture();

            // Set the MediaCapture to a variable in App.xaml.cs to handle suspension.
            (App.Current as App).MediaCapture = _mediaCapture;

            await _mediaCapture.InitializeAsync(_captureInitSettings);

            CreateProfile();
        }

        // Start the video capture.
        private async void StartMediaCaptureSession()
        {
            var storageFile = await Windows.Storage.KnownFolders.VideosLibrary.CreateFileAsync(
                "cameraCapture.mp4", Windows.Storage.CreationCollisionOption.GenerateUniqueName);

            await _mediaCapture.StartRecordToStorageFileAsync(_profile, storageFile);
            _recording = true;
        }

        // Stop the video capture.
        private async void StopMediaCaptureSession()
        {
            await _mediaCapture.StopRecordAsync();
            _recording = false;
            (App.Current as App).IsRecording = false;
        }


        private async void ShowFireFlyStatusBar()
        {
            //this will show the Status Bar  


            Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode.UseVisible);
            StatusBarObject.ProgressIndicator.Text = StatusBarHeader;
            StatusBarObject.ProgressIndicator.ProgressValue = 0;

            StatusBarObject.ForegroundColor = Colors.MintCream;
            StatusBarObject.BackgroundColor = Color.FromArgb(255, 166, 62, 59);
            StatusBarObject.BackgroundOpacity = .6;
            await StatusBarObject.ProgressIndicator.ShowAsync();


        }  

        /// <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 void OnNavigatedTo(NavigationEventArgs e)
        {
            ShowFireFlyStatusBar();
        }

        private void Camcorder_StartCapture_Click(object sender, RoutedEventArgs e)
        {
            StartMediaCaptureSession();
        }

        private void Camcorder_StopCapture_Click(object sender, RoutedEventArgs e)
        {
            StopMediaCaptureSession();
        }
    }

当我从照片应用程序获取视频文件时,它工作正常。

现在如何在应用中录制时启用实时预览?使用哪个控件?

【问题讨论】:

  • 我添加了相同的代码,它是第一次工作。但不是在下一次。甚至相机捕捉任务在我的应用程序中也不起作用。是否需要添加额外的代码?
  • 是因为我在处理完成后没有添加_mediaCapture.Dispose()。现在工作正常。 :)

标签: c# windows-phone-8 windows-runtime windows-phone-8.1 video-recording


【解决方案1】:

您正在寻找CaptureElement。将其添加到 XAML 后,将其连接到 MediaCapture 对象并开始预览,如下所示:

PreviewControl.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();

查看发布在 Microsoft github 页面上的 UniversalCameraSample 以获取更多信息。它面向 Windows 10,但大多数模式也应该适用于 8/8.1。

【讨论】:

  • CaptureElement 在 WIndows Phone 8.1 Silverlight 应用程序中不可用。有没有其他选择?
  • 一个Canvas 和一个VideoBrushmsdn.microsoft.com/en-us/library/windows/apps/…
  • 是的,这也是一种方式,但我已经通过矩形和 videoBrush 实现了这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
  • 2015-02-25
相关资源
最近更新 更多