Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaElement" /> class.
        /// </summary>
        public MediaElement()
            : base()
        {
            ContentGrid = new Grid {
                Name = nameof(ContentGrid)
            };
            Content = ContentGrid;
            ContentGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
            ContentGrid.VerticalAlignment   = VerticalAlignment.Stretch;
            ContentGrid.Children.Add(ViewBox);
            Stretch          = ViewBox.Stretch;
            StretchDirection = ViewBox.StretchDirection;
            Logger           = new GenericMediaLogger <MediaElement>(this);

            mediaElementCore = new MediaElementCore(this, WPFUtils.IsInDesignTime);

            if (WPFUtils.IsInDesignTime)
            {
                // Shows an FFmpeg image if we are in design-time
                var bitmap       = Properties.Resources.FFmpegMediaElementBackground;
                var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                var controlBitmap = new WriteableBitmap(bitmapSource);
                ViewBox.Source = controlBitmap;
            }
            else
            {
                // Bind to RoutedEvent events
                mediaElementCore.MediaOpening     += (s, e) => RaiseMediaOpeningEvent();
                mediaElementCore.MediaOpened      += (s, e) => RaiseMediaOpenedEvent();
                mediaElementCore.MediaClosed      += (s, e) => RaiseMediaClosedEvent();
                mediaElementCore.MediaFailed      += (s, e) => RaiseMediaFailedEvent(e.Exception);
                mediaElementCore.MediaEnded       += (s, e) => RaiseMediaEndedEvent();
                mediaElementCore.BufferingStarted += (s, e) => RaiseBufferingStartedEvent();
                mediaElementCore.BufferingEnded   += (s, e) => RaiseBufferingEndedEvent();
                mediaElementCore.SeekingStarted   += (s, e) => RaiseSeekingStartedEvent();
                mediaElementCore.SeekingEnded     += (s, e) => RaiseSeekingEndedEvent();

                // Bind to non-RoutedEvent events
                mediaElementCore.MessageLogged   += (o, e) => MessageLogged?.Invoke(this, e);
                mediaElementCore.PositionChanged += (s, e) => PositionChanged?.Invoke(this, e);

                // Bind to INotifyPropertyChanged event: PropertyChanged
                mediaElementCore.PropertyChanged += MediaElementCore_PropertyChanged;
            }

            m_Metadata = CollectionViewSource.GetDefaultView(mediaElementCore.Metadata) as ICollectionView;
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaElement" /> class.
        /// </summary>
        public MediaElement()
            : base()
        {
            Content          = ViewBox;
            Stretch          = ViewBox.Stretch;
            StretchDirection = ViewBox.StretchDirection;
            Logger           = new GenericMediaLogger <MediaElement>(this);
            Commands         = new MediaCommandManager(this);

            if (Utils.IsInDesignTime)
            {
                // Shows an FFmpeg image if we are in design-time
                var bitmap       = Properties.Resources.FFmpegMediaElementBackground;
                var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero,
                                                                         Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                var targetBitmap = new WriteableBitmap(bitmapSource);
                ViewBox.Source = targetBitmap;
            }
            else
            {
                // The UI Property update timer is responsible for timely updates to properties outside of the worker threads
                UIPropertyUpdateTimer = new DispatcherTimer(DispatcherPriority.DataBind)
                {
                    Interval  = Constants.UIPropertyUpdateInterval,
                    IsEnabled = true
                };

                // The tick callback performs the updates
                UIPropertyUpdateTimer.Tick += (s, e) =>
                {
                    UpdatePosition(IsOpen ? Clock?.Position ?? TimeSpan.Zero : TimeSpan.Zero);

                    var downloadProgress = Math.Min(1d, Math.Round((Container?.Components.PacketBufferLength ?? 0d) / DownloadCacheLength, 3));
                    if (double.IsNaN(downloadProgress))
                    {
                        downloadProgress = 0;
                    }
                    DownloadProgress = downloadProgress;
                };

                // Go ahead and fire up the continuous updates
                UIPropertyUpdateTimer.Start();
            }

            m_MetadataBase = new ObservableCollection <KeyValuePair <string, string> >();
            m_Metadata     = CollectionViewSource.GetDefaultView(m_MetadataBase) as ICollectionView;
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaElement" /> class.
        /// </summary>
        public MediaElement()
            : base()
        {
            ContentGrid = new Grid {
                Name = nameof(ContentGrid)
            };
            Content = ContentGrid;
            ContentGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
            ContentGrid.VerticalAlignment   = VerticalAlignment.Stretch;
            ContentGrid.Children.Add(ViewBox);
            Stretch          = ViewBox.Stretch;
            StretchDirection = ViewBox.StretchDirection;
            Logger           = new GenericMediaLogger <MediaElement>(this);
            Commands         = new MediaCommandManager(this);

            if (Utils.IsInDesignTime)
            {
                // Shows an FFmpeg image if we are in design-time
                var bitmap       = Properties.Resources.FFmpegMediaElementBackground;
                var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                var controlBitmap = new WriteableBitmap(bitmapSource);
                ViewBox.Source = controlBitmap;
            }
            else
            {
                // The UI Property update timer is responsible for timely updates to properties outside of the worker threads
                // We use the loaded priority because it is the priority right below the Render one.
                UIPropertyUpdateTimer = new DispatcherTimer(DispatcherPriority.Loaded)
                {
                    Interval  = Constants.UIPropertyUpdateInterval,
                    IsEnabled = true
                };

                // The tick callback performs the updates
                UIPropertyUpdateTimer.Tick += (s, e) =>
                {
                    UpdatePosition(IsOpen ? Clock?.Position ?? TimeSpan.Zero : TimeSpan.Zero);

                    if (HasMediaEnded == false && CanReadMorePackets && (IsOpening || IsOpen))
                    {
                        var bufferedLength = Container?.Components?.PacketBufferLength ?? 0d;
                        BufferingProgress = Math.Min(1d, bufferedLength / BufferCacheLength);
                        var oldIsBugffering = IsBuffering;
                        var newIsBuffering  = bufferedLength < BufferCacheLength;

                        if (oldIsBugffering == false && newIsBuffering == true)
                        {
                            RaiseBufferingStartedEvent();
                        }
                        else if (oldIsBugffering == true && newIsBuffering == false)
                        {
                            RaiseBufferingEndedEvent();
                        }

                        IsBuffering = HasMediaEnded == false && newIsBuffering;
                    }
                    else
                    {
                        BufferingProgress = 0;
                        IsBuffering       = false;
                    }

                    var downloadProgress = Math.Min(1d, Math.Round((Container?.Components.PacketBufferLength ?? 0d) / DownloadCacheLength, 3));
                    if (double.IsNaN(downloadProgress))
                    {
                        downloadProgress = 0;
                    }
                    DownloadProgress = downloadProgress;
                };

                // Go ahead and fire up the continuous updates
                UIPropertyUpdateTimer.Start();
            }

            m_MetadataBase = new ObservableCollection <KeyValuePair <string, string> >();
            m_Metadata     = CollectionViewSource.GetDefaultView(m_MetadataBase) as ICollectionView;
        }