13

我的 c# windows 窗体可以播放 mp3 文件。我使用此代码执行此操作

    WMPLib.WindowsMediaPlayer wplayer;
    wplayer = new WMPLib.WindowsMediaPlayer();
    wplayer.URL = "c:/Standup.mp3";
    wplayer.controls.play();

这很好用,但我想知道文件何时完成播放,以便我可以重新启动它。

请问我该怎么做?

4

4 回答 4

16

您可以通过使用PlayStateChanged 事件来做到这一点。您可以像这样将它添加到您的 MediaPlayer。

WMPLib.WindowsMediaPlayer wplayer;
wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);
wplayer.URL = "c:/Standup.mp3";
wplayer.controls.play();

然后,您可以在 EventHandler 中检查MediaEnded PlayState并将 currentPosition 重置为歌曲的开头:

void wplayer_PlayStateChange(int NewState)
{
    if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded)
    {
        wplayer.controls.currentPosition = 0;
    }
}

编辑:我希望能够让一首歌曲可重复到我厌倦的程度,当我设置断点时,上面的代码确实有效。删除它们后,我发现还有其他 PlayStates 正在停止播放文件。我可以通过使用一次性计时器绕过它。现在我厌倦了我正在使用的歌曲。可能/可能有更好的方法来做到这一点,但这会奏效。

修改后的代码

public partial class Form1 : Form
{
    WMPLib.WindowsMediaPlayer wplayer;
    Timer tmr = new Timer();
    public Form1()
    {
        InitializeComponent();
        tmr.Interval = 10;
        tmr.Stop();
        tmr.Tick += new EventHandler(tmr_Tick);
        wplayer = new WMPLib.WindowsMediaPlayer();
        wplayer.URL = "c:/Standup.mp3";
        wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);
        wplayer.controls.play();
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        tmr.Stop();
        wplayer.controls.play();
    }

    void wplayer_PlayStateChange(int NewState)
    {
        if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded )
        {
            tmr.Start();

        }
    }


}
于 2013-07-15T00:17:48.673 回答
6

如果您不一定需要知道文件何时完成以用于循环以外的任何其他目的,您可以考虑使用 setMode 方法打开轨道循环。

http://msdn.microsoft.com/en-us/library/windows/desktop/dd564867(v=vs.85).aspx

于 2013-07-14T23:28:19.327 回答
2

您可以不断检查它,Thread但是,几乎没有文档...

    //player .playState
    //Possible Values
    //
    //This property is a read-only Number (long). The C-style enumeration constant can be derived by prefixing 
    //the state value with "wmpps". For example, the constant for the Playing state is wmppsPlaying.
    //Value State Description
    //0     Undefined       Windows Media Player is in an undefined state.
    //1     Stopped         Playback of the current media item is stopped.
    //2     Paused          Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location.
    //3     Playing         The current media item is playing.
    //4     ScanForward     The current media item is fast forwarding.
    //5     ScanReverse     The current media item is fast rewinding.
    //6     Buffering       The current media item is getting additional data from the server.
    //7     Waiting         Connection is established, but the server is not sending data. Waiting for session to begin.
    //8     MediaEnded      Media item has completed playback.
    //9     Transitioning   Preparing new media item.
    //10    Ready           Ready to begin playing.
    //11    Reconnecting    Reconnecting to stream.
于 2015-11-22T08:43:08.563 回答
0

您可以使用媒体播放器内置的 PlayStateChange(int NewState) 函数来检测停止状态。

于 2013-07-14T23:24:22.730 回答