0

I just want to change the position of the gif in the MediaElement, so when i don't hover it with the mouse it should display a certain image of the GIF (the selected position) and when i move the cursor on the MediaElement the GIF should start playing from position zero. But i am not able to change the position of the GIF at all.

It starts playing and i can pause it, but setting position and the stop() method have no influence at all.


XAML Code:

<MediaElement x:Name="mediaElement" Source="C:\temp\smartGif.gif"
         ScrubbingEnabled="True" Loaded="mediaElement_Loaded" 
         MouseLeave="mediaElement_MouseLeave" 
         MouseEnter="mediaElement_MouseEnter" 
         LoadedBehavior="Manual" 
         HorizontalAlignment="Left" 
         Height="600" 
         Width="800" 
         VerticalAlignment="Top"/>

Basic Code:

public UserWindow()
{
    InitializeComponent();
}

private void mediaElement_Loaded(object sender, RoutedEventArgs e)
{
    mediaElement.Play();
    mediaElement.Position = TimeSpan.FromMilliseconds(100);
    mediaElement.Pause();
}

private void mediaElement_MouseEnter(object sender, MouseEventArgs e)
{
    mediaElement.Play();
    mediaElement.Position = TimeSpan.Zero;
}

private void mediaElement_MouseLeave(object sender, MouseEventArgs e)
{
    mediaElement.Position = TimeSpan.FromMilliseconds(100);
    mediaElement.Pause();
}

Is it right that the MediaElement needs to play that the position can be changed?


Changes: As suggested i added this:

MediaFailed="mediaElement_MediaFailed"

and that:

    private void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        MessageBox.Show("failed");
    }

But it does not show up, i dont know what to do. Is the gif then working fine or what could cause this? Do i need to download gifs in a special way to ensure it supports normal features? I tried it with different gifs and its still not working.

L. Guthardt
  • 1,990
  • 6
  • 22
  • 44
  • Try to subscribe to [`MediaFailed`](https://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.mediafailed(v=vs.110).aspx) event to see, perhaps gif doesn't support [`Position`](https://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.position(v=vs.110).aspx) change. – Sinatr Jul 11 '17 at 11:52
  • I just tried your code, [this](https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif) gif is displayed and animated (when mouse is over) without problem. Setting `Position` doesn't works correctly (it either skip some frames or rewind, but never to beginning). – Sinatr Jul 11 '17 at 12:24
  • Almost the same happens to me. The animation works, so the Play() and Pause() methods work, but changing the position never works, so there is no rewinding at all. I experience the same that between the enter and leave events some frames get skipped, for some odd reason. – L. Guthardt Jul 11 '17 at 12:27
  • I was able to restart playing by setting `LoadedBehavior` to `Close` and then to `Pause` + setting `Source` again. That has heavy flicker though. If you want to set `Position`, I'd suggest to try [another approach](https://stackoverflow.com/q/210922/1997232). – Sinatr Jul 11 '17 at 12:59
  • Wow thank you, but that its quite akward to do to achieve this basic feature. But i wonder, why has no one yet reported any issue on that, everbody talks about that it works fine. I came across that link as well and because i could not import any WinForm objects no matter how hard i tried i choosed the MediaElement. Thinking it supports all features for almost all data types, but apparently not. – L. Guthardt Jul 11 '17 at 13:09
  • MediaElement is very thin wrapper around Windows Media Player. If you open your gif in Windows Media Player - do you see the same problem? – Evk Jul 11 '17 at 14:08
  • Wow i just tried it and you are right. Windows Media Player is playing the gif but changing the position is disabled. I am speechless...Seems like converting the gif to mp4 is the easiest way to use that instead, what do you think? – L. Guthardt Jul 11 '17 at 14:11
  • Depends on what are you trying to achieve. Because if you need to play gifs, and not just arbitrary gifs but say provided by users - converting them to mp4 is quite ugly solution :) If on the other hand you have several static gifs to display, and they rarely change - well why not. – Evk Jul 11 '17 at 15:03
  • I dont need to play only gifs, it should have been just an option. But as i changed my programm that the used gifs will be static and never change this ugly solution will do the job. – L. Guthardt Jul 12 '17 at 08:10

2 Answers2

0

Surprisingly no one on the internet has reported this yet and lots of pepople say that it´s working, but it is actually not possible to change the position of a gif running in a MediaElement. The normal way is above in my question, which works for *.mp4 for example, but not for gifs. To convince you the easy way try out to play a gif in the Windows Media Player. As the MediaElement is very thin wrapped around Windows Media Player you will see the same result, changing the position is disabled.

There is a very ugly way how to reset a gif to play it from the beginning, but i dont suggest to use it if you have other options. This can be applied to any event/trigger.

private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
    if (mediaElement.Source.ToString() == "file:///C:/temp/newGif1.gif")
    {
        mediaElement.Source = new Uri("C:\\temp\\newGif.gif");
        mediaElement.Play();
    }

    else
    {
        mediaElement.Source = new Uri("C:\\temp\\newGif1.gif");
        mediaElement.Play();
    }
}

The only way is to reset the source to start the gif from the beginning, but you need a copy of your gif, because if it is the same source the MediaElement won´t update the source. In generell when you set the source for a MediaElement you have to set the fullpath. In XAML you can choose the gif from anywhere on your pc, it does not need to be set as a resource in your project.

But setting the source in the normal code, requires the gif to bet set as a resource in the project. So normally it is not temp like in the example but rather something like C:\Users\UnknownUser\Documents\Visual Studio 2015\Projects\RandomTestProject\Ressources.

If you dont rely on using gifs i suggest you to use *.mp4, because it is easier to handle and works the expected way and you can convert your *.gif easy to *.mp4.

L. Guthardt
  • 1,990
  • 6
  • 22
  • 44
0

This is hardly the ideal solution but I've found that you can loop a .gif image by subscribing to the MediaEnded event and setting the Position property to TimeSpan.FromMilliseconds(1) then calling the Play method.

e.g.

// Subscribe to the event
mediaElement.MediaEnded += mediaElement_MediaEnded;
private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
    // set the `Position` property to a 1ms `TimeSpan` and `Play`
    mediaElement.Position = TimeSpan.FromMilliseconds(1);
    mediaElement.Play();
}

Calling Play after setting the TimeSpan to 0 was not working for me. I'm unsure of the underlying cause.

aaronedmistone
  • 929
  • 10
  • 17