c# WPF how to repeat MediaElement playback from mediaended event handler without declaring new source?

痴心易碎 提交于 2019-11-30 20:20:15

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();
}

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

And on MediaEnded event you should set new position of video that will be not equals to zero:

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

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...

Visal

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!