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

C# 如何检测mp3文件何时播放完毕,c#,winforms,C#,Winforms,我的c#windows窗体允许播放mp3文件。我使用以下代码实现了这一点 WMPLib.WindowsMediaPlayer wplayer; wplayer = new WMPLib.WindowsMediaPlayer(); wplayer.URL = "c:/Standup.mp3"; wplayer.controls.play(); 这工作得很好,但我想知道文件何时完成播放,以便我可以重新启动它 >我怎么做? 你可以使用Player StaseCcha

我的c#windows窗体允许播放mp3文件。我使用以下代码实现了这一点

    WMPLib.WindowsMediaPlayer wplayer;
    wplayer = new WMPLib.WindowsMediaPlayer();
    wplayer.URL = "c:/Standup.mp3";
    wplayer.controls.play();
这工作得很好,但我想知道文件何时完成播放,以便我可以重新启动它


>我怎么做?

你可以使用Player StaseCchange(int NeNSTATE)函数BultIt到媒体播放器来检测停止状态。

< P>如果你不需要知道文件在什么时候完成,而不是循环,那么你可以考虑SETMODE方法来打开轨道循环。


您可以使用。您可以像这样将其添加到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
,并将currentPosition重置为歌曲的开头:

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

        }
    }


}

您可以使用
线程不断地检查它
,但是,几乎没有文档

    //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.

我真的不明白那个链接。请你给我一个代码示例。我没有使用过这个库,但据我所知,你应该能够使用:wplayer.settings.setMode(“loop”,true);或wplayer.settings.setMode(“autoRewind”,true);