WPF媒体元素

发布于 2024-08-23 08:49:51 字数 100 浏览 2 评论 0原文

我有一个 MediaElement,但是当 MediaElement 的属性“position”发生变化时如何调用函数?

位置不是 DependencyProperty。

I have a MediaElement, but how can I call a function when the property "position" of MediaElement changes?

Position is not a DependencyProperty.

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

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

发布评论

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

评论(1

伴梦长久 2024-08-30 08:49:51

您可以使用 DispatchTimer。本文提供了有关如何实现此功能的一些很好的见解。 MediaElement 及更多使用 WPF

这是我从我正在开发的项目中获取的一些示例代码。它使用滑块控件显示视频的位置,并允许用户更改位置。

我也是个新手,所以可能有些地方是错误的(请随时在评论部分评论问题:)。

private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    medPlayer.Play();
    medPlayer.Stop();
    mTimer = new DispatcherTimer();
    mTimer.Interval = TimeSpan.FromMilliseconds(100);
    mTimer.Tick += new EventHandler(mTimer_Tick);
    mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
    if (!mIsDragging)
    {
        try
        {
            mTick = true;
            sldPosition.Value = medPlayer.Position.TotalMilliseconds;
        }
        finally
        {
            mTick = false;
        }
    }
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
    mIsDragging = true;
    medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
    mIsDragging = false;
    if (chkPlay.IsChecked.Value)
        medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    var pos = TimeSpan.FromMilliseconds(e.NewValue);
    lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
    if (!mTick)
    {
        medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
        if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
        {
            chkPlay.IsChecked = false;
            medPlayer.Stop();
        }
    }
}

You can use a DispatchTimer. This article provides some good insight on how to get this working. MediaElement and More with WPF.

Here is some sample code that I took from a project I'm working on. It shows the position of the video using a slider control and allows the user to change the position.

I'm a bit of a newbie too, so it is possible that some of it is wrong (feel free to comment on problems in the comments section :).

private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    medPlayer.Play();
    medPlayer.Stop();
    mTimer = new DispatcherTimer();
    mTimer.Interval = TimeSpan.FromMilliseconds(100);
    mTimer.Tick += new EventHandler(mTimer_Tick);
    mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
    if (!mIsDragging)
    {
        try
        {
            mTick = true;
            sldPosition.Value = medPlayer.Position.TotalMilliseconds;
        }
        finally
        {
            mTick = false;
        }
    }
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
    mIsDragging = true;
    medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
    mIsDragging = false;
    if (chkPlay.IsChecked.Value)
        medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    var pos = TimeSpan.FromMilliseconds(e.NewValue);
    lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
    if (!mTick)
    {
        medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
        if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
        {
            chkPlay.IsChecked = false;
            medPlayer.Stop();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文