c# WPF 如何在不声明新源的情况下从 mediaending 事件处理程序重复 MediaElement 播放?

发布于 2024-11-07 05:51:19 字数 697 浏览 6 评论 0 原文

我正在 WPF 中播放视频。我希望它循环播放,所以我所做的是当 mediaded 事件触发时,我播放我的视频。所以这会让我循环。问题是为什么我必须再次创建新的源?为什么我不能直接叫“玩”?

由于某种原因,我不想在 XAML 中执行此操作。

看看我的代码片段:

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

或者是否有一种正确的方法不是在 XAML 中而是在此处的 .cs 文件中循环?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

看海 2024-11-14 05:51:19

不要在 Media_Ended 处理程序开始时重置 Source,而是尝试将 Position 值设置回起始位置。 Position 属性是一个 TimeSpan 所以你可能想要类似......

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

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();
}
烟酉 2024-11-14 05:51:19

您甚至不需要将 LoadedBehavior 设置为手动,只需将其保留为播放即可。

并重置您在 MediaEnded 上的媒体位置。

视频的新位置应大于零

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

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);
}
隔岸观火 2024-11-14 05:51:19

我让它工作设置 UnloadedBehavior="Manual" 和以下代码

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

将位置设置为零不起作用......

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

楠木可依 2024-11-14 05:51:19

我认为你应该使用这段代码:

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

我希望这会有所帮助。

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.

花开浅夏 2024-11-14 05:51:19

您不必再次设置源。只需将媒体元素的位置设置为 media_ending 事件的开始

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

您可能需要将 MediaElement.LoadedBehavior 设置为手动

<强>编辑

我已经尝试加载带有REPEAT标签的asf文件并且它工作正常

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

但是我认为mediaelement的内置播放列表处理机制有一些缺陷。我建议遵循下面链接中提到的解决方法

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

如果您有任何问题,请在此处发表评论

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文