如何检测mp3文件播放结束。

13

我的C# Windows窗体无法播放MP3文件。我使用了以下代码实现:

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

这个代码已经完美运行,但是我想知道当文件播放结束后如何重新开始播放。

请问我该怎么做?

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

然后您可以在事件处理程序中检查 MediaEnded PlayState ,并将当前位置重置为歌曲的开头:

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

编辑:我本来期望能够把一首歌曲重复播放到让我厌倦的地步,上述代码在我设置了断点时确实有效。但是一旦我删除了它们,我发现还有其他播放状态导致文件无法播放。我通过使用一次性计时器来解决了这个问题......现在我已经厌倦了我使用的那首歌曲。可能/很可能有更好的方法来做到这一点,但这样也可以。

修改后的代码

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

        }
    }


}

6

我真的不理解那个链接...请问你能给一个代码示例吗? - user2509901
3
我尚未使用过这个库,但据我理解,您可以使用以下代码: wplayer.settings.setMode("loop", true); 或者 wplayer.settings.setMode("autoRewind", true); - Harrison Lambeth

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.

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接