2009-11-09 39 views
2

我有这个类我在一个网站上找到:如何使用C#暂停MP3文件?

class MP3Handler 
{ 
    private string _command; 
    private bool isOpen; 
    [DllImport("winmm.dll")] 

    private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback); 

    public void Close() 
    { 
     _command = "close MediaFile"; 
     mciSendString(_command, null, 0, IntPtr.Zero); 
     isOpen = false; 
    } 

    public void Open(string sFileName) 
    { 
     _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile"; 
     mciSendString(_command, null, 0, IntPtr.Zero); 
     isOpen = true; 
    } 

    public void Play(bool loop) 
    { 
     if (isOpen) 
     { 
      _command = "play MediaFile"; 
      if (loop) 
       _command += " REPEAT"; 
      mciSendString(_command, null, 0, IntPtr.Zero); 
     } 
    } 
} 

是有停止和播放的方法。我想知道是否有人熟悉winmm.dll库。如何在播放歌曲时暂停歌曲,然后从暂停的地方继续播放歌曲?

+0

,做播放,暂停等,使用项目WINMM.DLL:http://www.codeproject.com/KB/audio-video/ Audio_Player__with_Winmm.aspx – 2009-11-09 23:58:35

回答

2

This CodeProject上的文章包含处理的WINMM.DLL库的功能,包括暂停5类,并可以为这个和是有帮助未来。

的基本代码,却是:

_command = "pause MediaFile"; 
    mciSendString(_command, null, 0, IntPtr.Zero); 
0

这里是所有的Multimedia Command Strings

我看到pauseresume命令都包含:

暂停命令暂停播放或记录 。大多数驱动程序保留 当前位置,并最终在此 位置恢复 播放或录制。 CD音频,数字视频, MIDI音序器,VCR,videodisc和 波形音频设备识别此 命令。

恢复命令继续播放 或记录已 使用暂停命令暂停的设备上。数字视频,VCR和波形音频 设备识别此命令。 尽管CD音频,MIDI音序器和 videodisc设备也可以识别此 命令,但MCICDA,MCISEQ和 MCIPIONR设备驱动程序不支持 它。

所以我想你会:

bool isPaused = false; 

public void Pause() { 
    if (isOpen && !isPaused) { 
     _command = "pause MediaFile"; 
     mciSendString(_command, null, 0, IntPtr.Zero); 
     isPaused = true; 
    } 
} 

public void Resume() { 
    if (isOpen && isPaused) { 
     _command = "resume MediaFile"; 
     mciSendString(_command, null, 0, IntPtr.Zero); 
     isPaused = false; 
    } 
}