unosquare/ffmediaelement

Calling MediaElement.Open from an OnLoaded event causes AccessViolation

seafraf opened this issue · 1 comments

Calling MediaElement.Open from an OnLoaded event causes AccessViolation

This is not an urgent issue, hoping by reporting this that it helps discover underlying issues for other people's problems.

I have an application where I have a list of videos to choose from and the first one from the list would have been loaded when the application starts with a call to MediaElement.Open, subsequent calls to MediaElement.Open would cause the application to crash wiht an AccessViolation (native memory issue, ffmpeg related).

In my tests:

  • Delaying the load of the first video by any amount of time fixes this, making the user click the first video themselves fixes my problem, also adding an arbitrary delay to the Loaded event fixes this issue (tested with Task.Delay with a value as low as 1ms)
  • Closing the video seems to work, so I don't think it's to do with any attempts to free memory when changing videos
  • Launching the sample application with a path to the video does not cause a crash when changing videos, I am assuming this is because of how the sample application loads videos using DelegateCommand's

Is this issue related to why the Source property is no longer favored?

Issue Categories

  • Bug/Crash

Version Information

Steps to Reproduce

  1. Create MediaElement with OnLoaded event
  2. Load a video with MediaElement.Open from the OnLoaded event
  3. Load another video at any time in the future
  4. Application crashes

Expected Results

  • No crash

Sample Code

XAML

<MainWindow>
        <ffme:MediaElement x:Name="videoPlayer" LoadedBehavior="Manual" UnloadedBehavior="Manual" Loaded="OnLoaded"  />
        <Button Click="OnButton" />
</MainWindow>

C#

private async void OnLoaded(object _, RoutedEventArgs _)
{
    // video loads fine
    await videoPlayer.Open("a.mp4")
}

private async void OnButton(object _, RoutedEventArgs _)
{
    // video loads preview but then crashes in a second or less
    await videoPlayer.Open("b.mp4")
}
<MainWindow>
        <ffme:MediaElement x:Name="videoPlayer" LoadedBehavior="Play" UnloadedBehavior="Manual" Loaded="OnLoaded"  />
        <Button Click="OnButton" />
`</MainWindow>`
private async void OnLoaded(object _, RoutedEventArgs _)
{
    // video loads fine
    await videoPlayer.Open("a.mp4")
}

private async void OnButton(object _, RoutedEventArgs _)
{
    // Try it's
    await videoPlayer.Close();
    // video loads preview but then crashes in a second or less
    await videoPlayer.Open("b.mp4");
}

If the source of the media has not changed, you can try this method 'await videoPlayer.Play()'

// Add in OnLoaded or Init fuction,check out panel in IDE
MediaElement.FFmpegMessageLogged += (s, e) =>
{
if (e.MessageType != MediaLogMessageType.Warning && e.MessageType != MediaLogMessageType.Error) return;
if (string.IsNullOrWhiteSpace(e.Message) == false && e.Message.ContainsOrdinal("Using non-standard frame rate")) return;
Debug.WriteLine(e);
};