WPF:实现MediaPlayer的音频/视频导引头 [英] WPF: Implementing a MediaPlayer Audio / Video Seeker

查看:366
本文介绍了WPF:实现MediaPlayer的音频/视频导引头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个MP3播放器(在WPF应用程序)与WPF 的MediaPlayer 基本上,我想实现一个宋导引头沿着与当前移动正在播放的歌曲。

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.

我已经实现了一个滑块歌(从<一个href=\"http://www.$c$cproject.com/KB/WPF/WPF%5FMedia%5FPlayer.aspx?fid=459289&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26#xx0xx\"相对=nofollow>萨沙理发的申请)和它的作品当用​​户手动拖动导引头(如,歌曲从那个位置继续),但我无法弄清楚如何根据当前使导引头动在歌曲的位置。

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.

麻烦的是,我不认为有一种方法来检查时的位置属性的MediaPlayer 有改变了,所以我难倒我应该如何实现此功能。

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.

这是如何去这样的问题?任何想法

Any ideas on how to go about such an issue?

[更新]

至于递增一个带有计时器的导引头,其实我想用我没有尝试,但其原因是因为我觉得这是实施这一使用 MediaTimeline ...但我还没有想出如何。

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.

推荐答案

ARISE答案!并为你的主

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.

下面是一个WPF应用程序的code-的背后,有着一个暂停/播放键。

Here is the code-behind of a WPF application, with a Pause/Play button.

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

诀窍是,一旦你设置的MediaPlayer的时钟,就变成时钟控制,因而使用MPlayer.Clock.Controller做所有的控制的:)

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

这篇关于WPF:实现MediaPlayer的音频/视频导引头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆