Commit c98941d7 authored by Martin Finkel's avatar Martin Finkel

Add Loaded event to alleviate LibVLCSharp.Forms.WPF timing issue with HWND creation

parent f4956e8b
......@@ -12,11 +12,21 @@ namespace LibVLCSharp.Forms.Shared
/// </summary>
public event EventHandler<MediaPlayerChangedEventArgs> MediaPlayerChanged;
/// <summary>
/// Raised after the first mediaplayer has been set.
/// This is mostly needed with LibVLCSharp.Forms.WPF to prevent timing issues regarding HWND creation.
/// It is safe to call Play() when Loaded has been raised.
/// </summary>
public event EventHandler Loaded;
public static readonly BindableProperty MediaPlayerProperty = BindableProperty.Create(nameof(MediaPlayer),
typeof(LibVLCSharp.Shared.MediaPlayer),
typeof(VideoView),
propertyChanged: OnMediaPlayerChanged);
/// <summary>
/// The MediaPlayer object attached to this view
/// </summary>
public LibVLCSharp.Shared.MediaPlayer MediaPlayer
{
get { return GetValue(MediaPlayerProperty) as LibVLCSharp.Shared.MediaPlayer; }
......@@ -28,6 +38,9 @@ namespace LibVLCSharp.Forms.Shared
var videoView = (VideoView)bindable;
Trace.WriteLine("OnMediaPlayerChanged");
videoView.MediaPlayerChanged?.Invoke(videoView, new MediaPlayerChangedEventArgs(oldValue as LibVLCSharp.Shared.MediaPlayer, newValue as LibVLCSharp.Shared.MediaPlayer));
if(oldValue == null && newValue != null) // assuming this is the first MediaPlayer set
videoView.Loaded?.Invoke(videoView, EventArgs.Empty);
}
}
}
\ No newline at end of file
......@@ -18,12 +18,18 @@ namespace LibVLCSharp.Forms.Sample
base.OnAppearing();
videoView.MediaPlayerChanged += VideoView_MediaPlayerChanged;
videoView.Loaded += VideoView_Loaded;
_vm = BindingContext as MainViewModel;
_vm.PropertyChanged += Vm_PropertyChanged;
_vm.Initialize();
}
private void VideoView_Loaded(object sender, System.EventArgs e)
{
_vm.MediaPlayer.Play();
}
private void Vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if(e.PropertyName.Equals(nameof(_vm.MediaPlayer)))
......
......@@ -35,8 +35,6 @@ namespace LibVLCSharp.Forms.Sample
"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4",
Media.FromType.FromLocation)
};
MediaPlayer.Play();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment