2010-10-11 59 views
1

根据我的朋友here最后一个职位建议这样的代码:如何在C#中重复MIDI文件?

using System; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.IO; 

namespace TeaTimer 
{ 
    /// <summary> 
    /// MCIPlayer is based off code by Slain. 
    /// Found here: http://www.sadeveloper.net/Articles_View.aspx?articleID=212 
    /// </summary> 
    public class MCIPlayer 
    { 
     private static readonly string sAlias="TeaTimerAudio"; 

     [DllImport("winmm.dll")] 
     private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback); 
     [DllImport("Winmm.dll")] 
     private static extern long PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags); 

     public static void Play(string sFile) 
     { 
      _Open(sFile); 
      _Play(); 
     } 
     public static void Stop() 
     { 
      _Close(); 
     } 

     private static void _Open(string sFileName) 
     { 
      if(_Status()!="") 
       _Close(); 

      string sCommand = "open \"" + sFileName + "\" alias "+sAlias; 
      mciSendString(sCommand, null, 0, IntPtr.Zero); 
     } 

     private static void _Close() 
     { 
      string sCommand = "close "+sAlias; 
      mciSendString(sCommand, null, 0, IntPtr.Zero); 
     } 

     private static void _Play() 
     { 
      string sCommand = "play "+sAlias; 
      mciSendString(sCommand, null, 0, IntPtr.Zero); 
     } 

     private static string _Status() 
     { 
      StringBuilder sBuffer = new StringBuilder(128); 
      mciSendString("status "+sAlias+" mode", sBuffer, sBuffer.Capacity, IntPtr.Zero); 
      return sBuffer.ToString(); 
     } 
    } 
} 

它工作正常,但现在的问题是,我不能重复我的MIDI文件。 我看到了一些代码,但我不知道为什么它不起作用。 我试过了:

Scommand = "play "+sAlias+" repeat "; 
mciSendString(Scommand, null, 0, IntPtr.Zero); 

回答

2

你为什么认为“重复”是一个支持的命令? 根据MSDN我看不到它被支持。

我看到的解决方案是使用notify flag

这里正在样品对我来说:

public class MCIPlayer 
{ 
    private class Form2: Form 
    { 
     public Form2() 
     { 
      if (!IsHandleCreated) CreateHandle(); 
     } 

     private const int MM_MCINOTIFY = 953; 

     protected override void WndProc(ref Message m) 
     { 
      base.WndProc(ref m); 
      if (m.Msg == MM_MCINOTIFY) 
       MCIPlayer.Play(file); 
     } 

     public string file; 
    } 

    private static Form2 f = new Form2(); 

    private static readonly string sAlias = "TeaTimerAudio"; 

    [DllImport("winmm.dll", SetLastError = true)] 
    private static extern int mciSendString(string strCommand, 
        StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback); 
    [DllImport("Winmm.dll")] 
    private static extern long PlaySound(byte[] data, IntPtr hMod, UInt32 dwFlags); 

    public static void Play(string sFile) 
    { 
     _Open(sFile); 
     _Play(); 
    } 

    public static void PlayTwice(string sFile) 
    { 
     _Open(sFile); 
     f.file = sFile; 
     _PlayTwice(); 
    } 

    public static void Stop() 
    { 
     _Close(); 
    } 

    private static void _Open(string sFileName) 
    { 
     if (_Status() != "") 
      _Close(); 

     string sCommand = "open \"" + sFileName + "\" alias " + sAlias; 
     mciSendString(sCommand, null, 0, IntPtr.Zero); 
    } 

    private static void _Close() 
    { 
     string sCommand = "close " + sAlias; 
     mciSendString(sCommand, null, 0, IntPtr.Zero); 
    } 

    private static void _Play() 
    { 
     string sCommand = "play " + sAlias; 
     mciSendString(sCommand, null, 0, IntPtr.Zero); 
    } 

    private static void _PlayTwice() 
    { 
     string sCommand = "play " + sAlias + " notify"; 
     mciSendString(sCommand, null, 0, f.Handle); 
    } 

    private static string _Status() 
    { 
     StringBuilder sBuffer = new StringBuilder(128); 
     mciSendString("status " + sAlias + " mode", sBuffer, 
           sBuffer.Capacity, IntPtr.Zero); 
     return sBuffer.ToString(); 
    } 
} 
+0

我应该怎么做重复2次以上plz帮助我:(( – Amir 2010-11-13 21:58:06

+0

我有更多的一个问题,我用它在我的框架,我的意思是我有一个其他的框架,但在我关闭框架后,我的声音盯着一段时间 – Amir 2010-11-13 22:01:59

+0

@Amir你可以把一个变量存储在歌曲重复的时间,并检查WndProc()内的变量,以确定是否需要调用MCIPlayer。再次播放(文件) – 2017-06-20 16:20:11