Esempio n. 1
0
        private async void OpenVideoFileAndPlay(StorageFile videoFile)
        {
            var videoStream = await videoFile.OpenAsync(FileAccessMode.Read);

            VideoElement.SetSource(videoStream, videoFile.ContentType);
            nameBlock.Text = videoFile.DisplayName;
        }
Esempio n. 2
0
        private async void PageLoaded(object sender, RoutedEventArgs e)
        {
            VideoElement.MediaOpened += (s, args) =>
            {
                Progress.Maximum   = VideoElement.NaturalDuration.TimeSpan.TotalSeconds;
                DurationLabel.Text = VideoElement.NaturalDuration.TimeSpan.ToString(@"hh\:mm\:ss");
            };
            IRandomAccessStream source = null;

            do
            {
                source = await GetSource();
            } while (source == null);
            VideoElement.SetSource(source, "");
            Progress.Minimum = 0;
            Progress.IsThumbToolTipEnabled = false;
            _timer = new DispatcherTimer()
            {
                Interval = TimeSpan.FromSeconds(0.2)
            };
            _timer.Tick += UpdateVideoPosition;
            _timer.Start();

            SystemNavigationManager.GetForCurrentView().BackRequested += NavigateBackRequested;
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                Frame.CanGoBack ?
                AppViewBackButtonVisibility.Visible :
                AppViewBackButtonVisibility.Collapsed;
        }
        /// <summary>
        /// Handle the returned file from file picker
        /// This method is triggered by ContinuationManager based on ActivationKind
        /// </summary>
        /// <param name="args">File save picker continuation activation argment. It cantains the file user selected with file save picker </param>
        public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
        {
            StorageFile videoFile = args.File;

            if (videoFile != null)
            {
                IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite);

                m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight);

                // Add frames to the video.
                ProcessVideoRing.IsActive = true;
                VideoElement.AreTransportControlsEnabled = false;
                ImageBtn.IsEnabled  = false;
                EncodeBtn.IsEnabled = false;
                statusText.Text     = "Encoding...";

                foreach (StorageFile file in m_files)
                {
                    Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync();

                    float scaleOfWidth  = (float)m_videoWidth / properties.Width;
                    float scaleOfHeight = (float)m_videoHeight / properties.Height;
                    float scale         = scaleOfHeight > scaleOfWidth ?
                                          scaleOfWidth : scaleOfHeight;
                    uint width  = (uint)(properties.Width * scale);
                    uint height = (uint)(properties.Height * scale);

                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                    {
                        for (int i = 0; i < 10; ++i)
                        {
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                            PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
                                BitmapPixelFormat.Bgra8,
                                BitmapAlphaMode.Straight,
                                new BitmapTransform { ScaledWidth = width, ScaledHeight = height },
                                ExifOrientationMode.RespectExifOrientation,
                                ColorManagementMode.ColorManageToSRgb);

                            m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height);
                        }
                    }
                }
                m_picture.Finalize();
                m_picture = null;

                VideoElement.AreTransportControlsEnabled = true;
                ImageBtn.IsEnabled        = true;
                EncodeBtn.IsEnabled       = true;
                statusText.Text           = "The image files are encoded successfully. You can review the video.";
                ProcessVideoRing.IsActive = false;

                videoStream.Dispose();
                videoStream = null;
                videoStream = await videoFile.OpenAsync(FileAccessMode.Read);

                VideoElement.SetSource(videoStream, videoFile.ContentType);
            }
        }
        private async void EncodeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (m_files.Count == 0)
            {
                statusText.Text = "You must select one image at least.";
                return;
            }

            // Create the video file via file picker.
            FileSavePicker savePicker = new FileSavePicker();

            savePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
            savePicker.FileTypeChoices.Add("MP4 File", new List <string>()
            {
                ".mp4"
            });
            savePicker.SuggestedFileName = "output";
            StorageFile videoFile = await savePicker.PickSaveFileAsync();

            if (videoFile != null)
            {
                IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite);

                m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight);

                // Add frames to the video.
                ProcessVideoRing.IsActive = true;
                statusText.Text           = "Encoding...";

                foreach (StorageFile file in m_files)
                {
                    Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync();

                    float scaleOfWidth  = (float)m_videoWidth / properties.Width;
                    float scaleOfHeight = (float)m_videoHeight / properties.Height;
                    float scale         = scaleOfHeight > scaleOfWidth ?
                                          scaleOfWidth : scaleOfHeight;
                    uint width  = (uint)(properties.Width * scale);
                    uint height = (uint)(properties.Height * scale);

                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                    {
                        for (int i = 0; i < 10; ++i)
                        {
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                            PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
                                BitmapPixelFormat.Bgra8,
                                BitmapAlphaMode.Straight,
                                new BitmapTransform { ScaledWidth = width, ScaledHeight = height },
                                ExifOrientationMode.RespectExifOrientation,
                                ColorManagementMode.ColorManageToSRgb);

                            m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height);
                        }
                    }
                }
                m_picture.Finalize();
                m_picture = null;

                statusText.Text           = "The image files are encoded successfully. You can review the video.";
                ProcessVideoRing.IsActive = false;

                videoStream.Dispose();
                videoStream = null;
                videoStream = await videoFile.OpenAsync(FileAccessMode.Read);

                VideoElement.SetSource(videoStream, videoFile.ContentType);
            }
        }