如何检测mp3文件何时播放完毕

时间:2013-07-14 23:16:47

标签: c# winforms

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

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

这完美无缺,但我想知道文件何时播放完毕,以便我可以重新启动它。

请问我该怎么做?

4 个答案:

答案 0 :(得分:16)

您可以使用PlayStateChanged event来完成此操作。你可以像这样将它添加到你的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();

        }
    }


}

答案 1 :(得分:6)

如果您不一定需要知道文件何时完成除了循环以外的任何其他目的,您可以考虑使用setMode方法来启用跟踪循环。

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

答案 2 :(得分: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.

答案 3 :(得分:0)

您可以使用内置于媒体播放器中的PlayStateChange(处于New State状态)功能来检测停止状态。