c# WPF how to repeat MediaElement playback from mediaended event handler without declaring new source?(c# WPF 如何在不声明新源的情况下从 mediaended 事件处理程序重复 MediaElement 播放?)
问题描述
我正在 WPF 中播放视频.我希望它循环播放,所以我所做的是当 mediaended 事件触发时,我播放我的视频.所以这会让我陷入困境.问题是为什么我必须再次创建新源?为什么我不能直接叫'play'?
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'?
出于某种原因,我不想在 XAML 中这样做.
I don't want to do it in XAML as for some reason.
看看我的代码片段:
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 文件中?
or is there a proper way to loop NOT in XAML but in here .cs file?
推荐答案
不要在 Media_Ended 处理程序的开头重置 Source,而是尝试将 Position 值设置回起始位置.Position 属性是一个 TimeSpan,因此您可能想要...
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();
}
这篇关于c# WPF 如何在不声明新源的情况下从 mediaended 事件处理程序重复 MediaElement 播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c# WPF 如何在不声明新源的情况下从 mediaended 事件处理程序重复 MediaElement 播放?
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
