first version
This commit is contained in:
@@ -11,21 +11,11 @@ namespace TV_Player
|
||||
/// </summary>
|
||||
public partial class VideoPlayer : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty SourceUrlProperty =
|
||||
DependencyProperty.Register(
|
||||
"SourceUrl", // Name of the property
|
||||
typeof(string), // Type of the property
|
||||
typeof(VideoPlayer), // Type of the owner class
|
||||
new PropertyMetadata(string.Empty) // Default value
|
||||
);
|
||||
public string SourceUrl
|
||||
{
|
||||
get { return (string)GetValue(SourceUrlProperty); }
|
||||
set { SetValue(SourceUrlProperty, value); }
|
||||
}
|
||||
public string SourceUrl { get; set; }
|
||||
|
||||
private LibVLC _libVLC;
|
||||
private MediaPlayer _mediaPlayer;
|
||||
private PlayerViewModel _viewModel;
|
||||
|
||||
public VideoPlayer()
|
||||
{
|
||||
@@ -33,6 +23,12 @@ namespace TV_Player
|
||||
|
||||
_libVLC = new LibVLC(enableDebugLogs: true);
|
||||
_mediaPlayer = new MediaPlayer(_libVLC);
|
||||
this.DataContextChanged += (sender, e) =>
|
||||
{
|
||||
_viewModel = (PlayerViewModel)e.NewValue;
|
||||
_viewModel.SourceUrlChangedEvent += _viewModel_SourceUrlChangedEvent;
|
||||
};
|
||||
|
||||
|
||||
VideoView.Loaded += (sender, e) =>
|
||||
{
|
||||
@@ -43,7 +39,15 @@ namespace TV_Player
|
||||
AutoPlay();
|
||||
};
|
||||
Unloaded += VideoPlayer_Unloaded;
|
||||
}
|
||||
|
||||
private void _viewModel_SourceUrlChangedEvent(string videoURL)
|
||||
{
|
||||
SourceUrl = videoURL;
|
||||
VideoView.MediaPlayer.Stop();
|
||||
VideoView.MediaPlayer.Media.Dispose();
|
||||
|
||||
AutoPlay();
|
||||
}
|
||||
|
||||
private void VideoView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
@@ -55,8 +59,6 @@ namespace TV_Player
|
||||
{
|
||||
VideoView.Dispose();
|
||||
}
|
||||
|
||||
|
||||
void PauseButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (VideoView.MediaPlayer.IsPlaying)
|
||||
@@ -64,28 +66,22 @@ namespace TV_Player
|
||||
VideoView.MediaPlayer.Pause();
|
||||
}
|
||||
}
|
||||
|
||||
private void AutoPlay()
|
||||
{
|
||||
if (!VideoView.MediaPlayer.IsPlaying)
|
||||
{
|
||||
|
||||
using (var media = new Media(_libVLC, new Uri(SourceUrl)))
|
||||
VideoView.MediaPlayer.Play(media);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void MyUserControl_MouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
ToggleOverlay();
|
||||
}
|
||||
|
||||
private void MyUserControl_TouchDown(object sender, TouchEventArgs e)
|
||||
{
|
||||
ToggleOverlay();
|
||||
}
|
||||
|
||||
private void ToggleOverlay()
|
||||
{
|
||||
if (overlayPanel.Visibility == Visibility.Visible)
|
||||
@@ -97,19 +93,17 @@ namespace TV_Player
|
||||
ShowOverlay();
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowOverlay()
|
||||
{
|
||||
overlayPanel.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
public void HideOverlay()
|
||||
{
|
||||
overlayPanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_viewModel.SourceUrlChangedEvent -= _viewModel_SourceUrlChangedEvent;
|
||||
VideoView.MediaPlayer?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user