2010-10-07 4 views
2

J'utilise ce code pour jouer un fichier MIDI pour mon jeu, mais je n'entends aucun son de mes haut-parleurs. Voulez-vous me aider? Il est une sorte d'urgence, s'il vous plaît ... Mes haut-parleurs sont allumés;)aucun son n'est entendu pendant la lecture d'un fichier MIDI en C# .Net

[DllImport("winmm.dll", EntryPoint="mciSendStringA")] 
private static extern long mciSendString(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback); 
public static long PlayMidiFile(string MidiFile) 
{ 
long lRet = -1; 

if (File.Exists(MidiFile)) 
{ 
    lRet = mciSendString("stop midi", "", 0, 0); 
    lRet = mciSendString("close midi", "", 0, 0); 
    lRet = mciSendString(("open sequencer!" 
    + (MidiFile + " alias midi")), "", 0, 0); 
    lRet = mciSendString("play midi", "", 0, 0); 
    return lRet; 
} 

else 
{ 
    //Error Message 
    return lRet; 
} 
} 
+0

Lire ceci: http://stackoverflow.com/questions/8763/best-way-to-play-midi-sounds-using-c, dès le sommet, il semble comme ta signature pinvoke est foiré. L'argument 2 est un tampon. – Mark

+0

Et args 3 et 4 sont faux aussi. Ceci a été copié à partir du code VB6. –

Répondre

4

Je ne suis pas vraiment sûr de votre mise en œuvre de winmm.dll mais j'ai un code de travail et testé pour elle. J'ai reçu le code source de ce projet open source: Tea Timer.

L'implémentation du code est assez simple comme ci-dessous. J'espère que cela aide.

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

EDIT: Voici comment lire et d'arrêter un fichier musical:

public static void playSound(string sFile) 
{ 
    //WavPlay.WavPlayer.Play(sFile); 
    MCIPlayer.Play(sFile); 
} 
public static void stopSound() 
{ 
    //WavPlay.WavPlayer.StopPlay(); 
    MCIPlayer.Stop(); 
} 
+0

merci bro, Il fonctionne bien dans mon ordinateur aussi.: X – Amir

+0

Content d'entendre que :) – bla

+0

HOW Je peux répéter mon MIDI Sy Sye? Est-ce que tu pourrais m'aider? J'ai trouvé si j'essaie d'ajouter chaîne Scommand = "play" + sAlias ​​+ "repeat"; mciSendString (Scommand, null, 0, IntPtr.Zero); mais il fonctionne dosent :( – Amir

1

Je l'habitude d'utiliser la définition ...

[DllImport("winmm.dll", EntryPoint = "mciSendStringA")] 

public static extern void mciSendStringA(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback); 

... en .Net 3.5 mais en. Net 4.0 m'a donné et exception déséquilibrée pinvoke! Je l'ai fixé en utilisant cette place ...

[DllImport("winmm.dll", EntryPoint = "mciSendStringA")] 

public static extern void mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback); 

... et en passant IntPtr.Zero comme le dernier param.

La seule différence est le uReturnLength est un int (et non un long) et hwndCallback est un IntPtr (et non un long).

Hope this helps ...