public MediaInfoPopup(MediaInfo currentMedia, MediaElement mediaPlayer) : this() { player = mediaPlayer; mediaPlayer.MediaChanging += MediaPlayer_MediaChanging; episode = currentMedia.ToMediaEpisode(); ShowEpisodeInfo(episode); }
public MediaElementUnoSquare(Unosquare.FFME.MediaElement mediaElement) { _mediaElement = mediaElement; _mediaElement.MediaOpened += HandleMediaOpened; _mediaElement.MediaClosed += HandleMediaClosed; _mediaElement.MediaEnded += HandleMediaEnded; _mediaElement.MediaFailed += HandleMediaFailed; _mediaElement.RenderingSubtitles += HandleRenderingSubtitles; _mediaElement.PositionChanged += HandlePositionChanged; _mediaElement.MessageLogged += HandleMessageLogged; }
private void Page_Loaded(object sender, RoutedEventArgs e) { PageMediaElement = new Unosquare.FFME.MediaElement { LoadedBehavior = MediaState.Play }; PageMediaElement.MediaEnded += PageMediaElement_MediaEnded; PageMediaElement.MediaOpened += PageMediaElement_MediaOpened; PageMediaElement.Source = new Uri($"{Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)}\\Assets\\Media\\Video\\Guilan Trailer.mp4"); PageFrame.Visibility = Visibility.Collapsed; App.CastedMainWindow.MusicPlayer.Open(new Uri($"{Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)}\\Assets\\Media\\Sound\\Music.mp3")); App.CastedMainWindow.SoundPlayerRepeatMode = true; }
private async Task ClearVideoElement(bool seek) { await Task.Run(async() => { await VideoElement.Stop(); Dispatcher.Invoke(() => { VideoElement.Position = new TimeSpan(0); VideoElement.Source = null; VideoElement.Dispose(); VideoElement = null; VideoElementBorder.Visibility = Visibility.Hidden; VideoElement = new Unosquare.FFME.MediaElement { IsMuted = true, LoadedBehavior = MediaState.Manual, Visibility = System.Windows.Visibility.Visible, }; VideoElement.MediaOpened += VideoElement_MediaOpened; VideoElement.MediaFailed += VideoElement_MediaFailed; VideoElement.MediaEnded += (sender, e) => { VideoElement.Position = TimeSpan.FromSeconds(0); }; if (seek) { VideoElement.SeekingStarted += (sender, e) => { }; VideoElement.SeekingEnded += (sender, e) => { if (!_videoPlay) { return; } PlayMedia(); }; } VideoElementBorder.Children.Add(VideoElement); }); }); }
void InitializePlayer() { LastMouseMoveTime = DateTime.UtcNow; Loaded += (s, e) => { Storyboard.SetTarget(HideControllerAnimation, MiniControllerPanel); Storyboard.SetTarget(ShowControllerAnimation, MiniControllerPanel); HideControllerAnimation.Completed += (es, ee) => { MiniControllerPanel.Visibility = Visibility.Hidden; IsControllerHideCompleted = true; }; ShowControllerAnimation.Completed += (es, ee) => { IsControllerHideCompleted = false; }; _me = (LayoutRoot.DataContext as PlayerViewModel).MediaPlayer; }; MouseMove += (s, e) => { var currentPosition = e.GetPosition(this); if (Math.Abs(currentPosition.X - LastMousePosition.X) > double.Epsilon || Math.Abs(currentPosition.Y - LastMousePosition.Y) > double.Epsilon) { LastMouseMoveTime = DateTime.UtcNow; } LastMousePosition = currentPosition; }; MouseLeave += (s, e) => { LastMouseMoveTime = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(10)); }; MouseMoveTimer = new DispatcherTimer(DispatcherPriority.Background) { Interval = TimeSpan.FromMilliseconds(150), IsEnabled = true }; MouseMoveTimer.Tick += (s, e) => { var elapsedSinceMouseMove = DateTime.UtcNow.Subtract(LastMouseMoveTime); if (elapsedSinceMouseMove.TotalMilliseconds >= 3000 && _me.IsOpen && MiniControllerPanel.IsMouseOver == false) { if (IsControllerHideCompleted) { return; } Cursor = Cursors.None; HideControllerAnimation?.Begin(); IsControllerHideCompleted = false; } else { Cursor = Cursors.Arrow; MiniControllerPanel.Visibility = Visibility.Visible; ShowControllerAnimation?.Begin(); } }; MouseMoveTimer.Start(); }
private void LoadPlayer() { var exts = new string[] { ".mp4", ".avi", ".webm", ".wmv", ".mov", ".mkv", ".flv", ".rmvb" }; var dir = AppDomain.CurrentDomain.BaseDirectory + "Videos"; #if DEBUG dir = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos); #endif videos = Directory.GetFiles(dir) .Where(f => exts.Contains(System.IO.Path.GetExtension(f))) .ToArray(); if (videos.Length < 1) { MessageBox.Show("未包含任何影片,请添加后重启!"); Process.Start(dir); this.Close(); return; } switch (AppSettings["Codec"].ToLower()) { case "ffmpeg": if (Environment.Is64BitProcess) { Unosquare.FFME.Library.FFmpegDirectory = AppDomain.CurrentDomain.BaseDirectory + "FFmpeg.4.1.1\\x64"; } else { Unosquare.FFME.Library.FFmpegDirectory = AppDomain.CurrentDomain.BaseDirectory + "FFmpeg.4.1.1\\x86"; } var player2 = new Unosquare.FFME.MediaElement { Volume = 1, Stretch = Stretch.Fill, LoadedBehavior = Unosquare.FFME.Common.MediaPlaybackState.Manual, Source = new Uri(videos[0], UriKind.Absolute) }; player2.BeginInit(); root.Children.Insert(1, player2); player2.MediaEnded += OnMediaEnded; player2.MediaOpening += OnMediaOpening; player2.MediaOpened += OnMediaOpened; player2.EndInit(); break; case "vlc": if (Environment.Is64BitProcess) { Meta.Vlc.Wpf.ApiManager.Initialize(@"LibVlc\x64", new string[] { "-I", "dummy", "--ignore-config", "--no-video-title", "--rtsp-tcp" }); } else { Meta.Vlc.Wpf.ApiManager.Initialize(@"LibVlc\x86", new string[] { "-I", "dummy", "--ignore-config", "--no-video-title", "--rtsp-tcp" }); } var player1 = new Meta.Vlc.Wpf.VlcPlayer { Volume = 1, Stretch = Stretch.Fill, }; player1.Initialized += (s, e) => { player1.LoadMedia(videos[0]); }; root.Children.Insert(1, player1); player1.StateChanged += OnStateChanged; player1.LengthChanged += OnLengthChanged; player1.VideoFormatChanging += (s, e) => { if (e.Width > this.ActualWidth && this.ActualWidth > 0) { var w = this.ActualWidth; var h = w * e.Height / e.Width; e.Width = (uint)w; e.Height = (uint)h; } }; break; default: var player3 = new MediaElement { Volume = 1, Stretch = Stretch.Fill, LoadedBehavior = MediaState.Manual, Source = new Uri(videos[0], UriKind.Absolute) }; player3.MediaEnded += OnMediaEnded; player3.MediaOpened += OnMediaOpened; root.Children.Insert(1, player3); break; } if (AppSettings["AutoPlay"].ToLower() == "true") { Task.Delay(200).ContinueWith((t) => { this.Dispatcher.Invoke(() => { this.Play(1); }); }); } }
private async Task SafelyRecreateVideoElement(bool showVideo) { await Task.Run(async() => { await VideoElement.Stop(); Dispatcher.Invoke(() => { VideoElement.Position = TimeSpan.Zero; VideoElement.Source = null; VideoElement.MediaOpened -= OnMediaOpened; VideoElement.MediaFailed -= OnMediaFailed; VideoElement.MediaEnded -= OnMediaEnded; VideoElement.SeekingStarted -= OnSeekingStarted; VideoElement.SeekingEnded -= OnSeekingEnded; //VideoElement.Dispose(); VideoElement = null; VideoElementBorder.Visibility = Visibility.Hidden; VideoElement = new Unosquare.FFME.MediaElement { IsMuted = true, LoadedBehavior = MediaPlaybackState.Manual, Visibility = Visibility.Visible, }; VideoElement.MediaOpened += OnMediaOpened; VideoElement.MediaFailed += OnMediaFailed; VideoElement.MediaEnded += OnMediaEnded; if (showVideo) { VideoElement.SeekingStarted += OnSeekingStarted; VideoElement.SeekingEnded += OnSeekingEnded; } VideoElementBorder.Children.Add(VideoElement); }); }); async void OnMediaOpened(object sender, MediaOpenedEventArgs e) { VideoElementBorder.Visibility = Visibility.Visible; if (!_videoPlay) { return; } await Task.Run(() => _waitAction?.Invoke()); if (VideoElement == null /* || VideoElement.IsDisposed*/) { return; } await VideoElement.Play(); VideoElement.Position = _position; } async void OnMediaFailed(object sender, MediaFailedEventArgs e) { VideoElementBorder.Visibility = Visibility.Hidden; //MsgBox.Show(this, e.ErrorException.ToString(), "不支持的视频格式", MessageBoxButton.OK, MessageBoxImage.Error); if (!_videoPlay) { return; } await SafelyRecreateVideoElement(false); PlayMedia(); } void OnMediaEnded(object sender, EventArgs e) { if (VideoElement == null /*|| VideoElement.IsDisposed*/) { return; } VideoElement.Position = TimeSpan.Zero; } void OnSeekingStarted(object sender, EventArgs e) { } void OnSeekingEnded(object sender, EventArgs e) { if (!_videoPlay) { return; } PlayMedia(); } }
public void ShowMediaInfoPopup(MediaInfo mediaInfo, MediaElement player) { mediaPopupBox.Content = new MediaInfoPopup(mediaInfo, player); }