Windows Phone 8.1中的录像机应用程序

时间:2015-04-13 05:23:44

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

我打算在 Windows Phone 8.1(RT)中编写录像机应用

我使用了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();
        }
    }

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

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

1 个答案:

答案 0 :(得分:2)

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

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

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