10

I'm playing a video in WPF.i want it to loop so what I did is when the mediaended event fires, I play back my video. so this will get me a loop. prob is why do u I have to create new source again? why can't I just call 'play'?

I don't want to do it in XAML as for some reason.

have a look at my code snippet:

string startPath System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);


public Window1()
    {
        InitializeComponent();
        media.Source = new Uri(startPath + @"playlist.wpl");
        media.play();
    }

private void Media_Ended(object sender, EventArgs e)
    {
        media.Source = new Uri(startPath + @"playlist.wpl"); //if i dont put this line, video wont play..seems like it cant get the source
        media.Play();
    }

or is there a proper way to loop NOT in XAML but in here .cs file?

5 Answers 5

21

Instead of resetting the Source at the start of your Media_Ended handler, try setting the Position value back to the start position. The Position property is a TimeSpan so you probably want something like...

private void Media_Ended(object sender, EventArgs e)
{
    media.Position = TimeSpan.Zero;
    media.Play();
}
3
  • thnx for your answer. i've tried this earlier but no success.video doesnt play back.
    – Psychocryo
    May 12, 2011 at 9:32
  • Hmmm...that's strange. I tested it, but with an audio file not a video. Not sure why video would be different - I'll take another look.
    – IanR
    May 12, 2011 at 9:36
  • 1
    For some reasons this combination doesnot working sometimes! In my case for gif.
    – Ievgen
    Oct 21, 2014 at 12:55
12

You don't even need to set LoadedBehavior as Manual just leave it Play.

And reset your media position on MediaEnded.

The new position of the video should be greater than zero:

private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
     media.Position = TimeSpan.FromMilliseconds(1);
}
7

I make it work setting the UnloadedBehavior="Manual" and the follwing code

 private void gifAnimation_MediaEnded(object sender, RoutedEventArgs e)
 {
    gifAnimation.Position = new TimeSpan(0,0,1);
    gifAnimation.Play();
 }

Setting the position to Zero didnt work...

1
  • 2
    this worked for me! setting to zero did not work. followed instructions here and worked for me. my case was an animated gif.
    – faldeland
    Apr 2, 2014 at 21:01
2

I think you should use this code :

private void Media_Ended(object sender, EventArgs e)
{
   media.Position = TimeSpan.Zero;
   media.LoadedBehavior = MediaState.Play;
}

I hope this will help.

2
  • Please don't provide "code only" answers. Why do you think this is the answer?
    – Lee Taylor
    Nov 17, 2012 at 0:05
  • 1
    This worked for me, you can't call "Play" if the LoadBehaviour has already been set to Play in the XAML. Jan 14, 2015 at 0:08
0

You don't have to set the source again.Just set the position of the mediaelement to the start on the media_ended event

 private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            ((MediaElement)(sender)).Stop();
            ((MediaElement)(sender)).Position = new TimeSpan(0, 0, 0);
            ((MediaElement)(sender)).Play();
        }

You may need to set MediaElement.LoadedBehavior to Manual

EDIT

I have tried loading an asf file with the REPEAT tag and its working fine

<ASX version = "3.0">
   <TITLE>Simple ASX Demo</TITLE>
<REPEAT> 
      <ENTRY>
         <TITLE>Vista Butterfly Video</TITLE>
         <AUTHOR>Microsoft Corporation</AUTHOR>
         <COPYRIGHT>(c)2007 Microsoft Corporation</COPYRIGHT>
         <REF HREF = "Butterfly.wmv" />
     </ENTRY>
</REPEAT> 
</ASX>

But i think the inbuilt playlist handling mechanism of mediaelement has some flaws.I recommend following the workaround mentioned in the below link

http://blog.revolunet.com/index.php/general/wpf-mediaelement-asx-workaround

Post comment here if you have any problem

5
  • it seems that it's working if the media is a single file but not working if i use a playlist file such as .wpl or .asx file format. any clue?
    – Psychocryo
    May 16, 2011 at 6:45
  • So after the entire playlist is finished...you want to repeat from the first file right ?
    – biju
    May 16, 2011 at 7:11
  • Also how do you manage your playlist ? AFIK The MediaElement does not have a built in playlist handling mechanism.
    – biju
    May 16, 2011 at 7:24
  • yeah.i want to play all from the beginning.so i did all the same as your instruction but the media source is a .wpl file. i tried an .asx file but the same result.it doesnt repeat back. if i use media source such as lexus.mp4, everything goes fine using ur instruction..any idea?
    – Psychocryo
    May 16, 2011 at 7:41
  • @biju,thnx mate.i'll give it a go and see how.thnx again.
    – Psychocryo
    May 19, 2011 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.