C# WPF:实现 MediaPlayer 音频/视频搜索器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/869761/
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
WPF: Implementing a MediaPlayer Audio / Video Seeker
提问by Andreas Grech
I am currently working on an MP3 player (in a WPF application) with a WPF MediaPlayer
and basically, I want to implement a Song Seeker which moves along with the current playing song.
我目前正在使用 WPF 开发 MP3 播放器(在 WPF 应用程序中),MediaPlayer
基本上,我想实现一个与当前播放的歌曲一起移动的歌曲搜索器。
I already implemented a song slider (from Sacha Barber's application) and it works when the user drags the seeker manually (as in, the song continues from that position) but I cannot figure out how to make the seeker move according to the current position in the song.
我已经实现了一个歌曲滑块(来自Sacha Barber 的应用程序),它在用户手动拖动搜索器时起作用(如,歌曲从该位置继续),但我无法弄清楚如何根据当前位置使搜索器移动这首歌。
Trouble is I don't think there is a way to check when the Position
property of the MediaPlayer
has changed, so I'm stumped as to how I should implement this feature.
问题是我认为没有办法检查 的Position
属性何时MediaPlayer
发生变化,所以我不知道应该如何实现这个功能。
Any ideas on how to go about such an issue?
关于如何解决此类问题的任何想法?
[Update]
[更新]
As regards incrementing the seeker with a timer, I actually thought of using the reason I didn't try it yet is because I think there is a better way to implement this using the MediaTimeline
...but I'm yet to figure out how.
至于用计时器增加搜索器,我实际上想到了使用我还没有尝试的原因是因为我认为有更好的方法来实现这个使用MediaTimeline
...但我还没有弄清楚如何。
采纳答案by Alastair Pitts
ARISE answer! and serve your master
起来回答!并为你的主人服务
OK, I've figured out how to work this. I'm sure I'm not doing it the completely correct way but it does work.
好的,我已经想出了如何工作。我确定我没有以完全正确的方式做这件事,但它确实有效。
Here is the code-behind of a WPF application, with a Pause/Play button.
这是 WPF 应用程序的代码隐藏,带有暂停/播放按钮。
public partial class Main : Window
{
MediaPlayer MPlayer;
MediaTimeline MTimeline;
public Main()
{
InitializeComponent();
var uri = new Uri("C:\Test.mp3");
MPlayer = new MediaPlayer();
MTimeline = new MediaTimeline(uri);
MTimeline.CurrentTimeInvalidated += new EventHandler(MTimeline_CurrentTimeInvalidated);
MPlayer.Clock = MTimeline.CreateClock(true) as MediaClock;
MPlayer.Clock.Controller.Stop();
}
void MTimeline_CurrentTimeInvalidated(object sender, EventArgs e)
{
Console.WriteLine(MPlayer.Clock.CurrentTime.Value.TotalSeconds);
}
private void btnPlayPause_Click(object sender, RoutedEventArgs e)
{
//Is Active
if (MPlayer.Clock.CurrentState == ClockState.Active)
{
//Is Paused
if (MPlayer.Clock.CurrentGlobalSpeed == 0.0)
MPlayer.Clock.Controller.Resume();
else //Is Playing
MPlayer.Clock.Controller.Pause();
}
else if (MPlayer.Clock.CurrentState == ClockState.Stopped) //Is Stopped
MPlayer.Clock.Controller.Begin();
}
}
The trick is that once you set the clock of a MediaPlayer, it becomes clock controlled, thus the use of MPlayer.Clock.Controller to do all of the controlling :)
诀窍是一旦你设置了 MediaPlayer 的时钟,它就会变成时钟控制的,因此使用 MPlayer.Clock.Controller 来完成所有的控制:)
回答by Crippeoblade
Never played with media player but assuming you know the length of song could you not setup a timer that ticks every second while the song is playing. Therefore for every tick just increment the seeker in relation to how long the song is in total.
从来没有玩过媒体播放器,但假设你知道歌曲的长度,你能不能设置一个在歌曲播放时每秒滴答一次的计时器。因此,对于每个滴答声,只需根据歌曲总长度增加搜索器。
Song is 100 seconds long. Therefore every second/tick is worth 1 percent of total progress.
歌曲长度为 100 秒。因此,每一秒/滴答的价值占总进度的 1%。
You'd have to stop the timer when pausing song etc...
暂停歌曲等时,您必须停止计时器...
回答by Sebastian Gray
MediaElement has a position property which you could use for this: http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.position.aspx
MediaElement 有一个位置属性,您可以使用它:http: //msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.position.aspx