如何在 C# WPF 中以重复模式运行 MediaPlayer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24320487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run the MediaPlayer on repeat mode in C# WPF?
提问by user3080029
I have a MediaPlayer
我有一个 MediaPlayer
public MediaPlayer backgroundMusicPlayer = new MediaPlayer ();
Now, because its background music, I'd like to repeat the song after its over.
现在,因为它的背景音乐,我想在它结束后重复这首歌。
How should I implement this?
我应该如何实施?
This is what I found:
这是我发现的:
An event is raised when the song ends: backgroundMusicPlayer.MediaEnded
歌曲结束时会引发一个事件: backgroundMusicPlayer.MediaEnded
I am not sure what I should do about this? I am new to C# and programming in general.
我不确定我应该怎么做?我是 C# 和一般编程的新手。
EDIT:
编辑:
public void PlaybackMusic ( )
{
if ( backgroundMusicFilePath != null )
{
backgroundMusicPlayer.Open (new Uri (backgroundMusicFilePath));
backgroundMusicPlayer.MediaEnded += new EventHandler (Media_Ended);
backgroundMusicPlayer.Play ();
return;
}
}
private void Media_Ended ( object sender, EventArgs e )
{
backgroundMusicPlayer.Open (new Uri (backgroundMusicFilePath));
backgroundMusicPlayer.Play ();
}
This works, but I need to open the file every time. Is this the only way?
这有效,但我每次都需要打开文件。这是唯一的方法吗?
回答by Robert Langdon
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.
不要在 Media_Ended 处理程序开始时重置 Source,而是尝试将 Position 值设置回开始位置。Position 属性是一个 TimeSpan,所以你可能想要类似的东西。
private void Media_Ended(object sender, EventArgs e)
{
media.Position = TimeSpan.Zero;
media.Play();
}
EDIT 1 :Find more details here
编辑 1:在此处查找更多详细信息

