开发者

no sound heard while playing a MIDI file in C#.Net

I use this code to play a MIDI file for my game, but I can not hear any sound from my speakers. Would you help me? It's kind of an emergency, please... My speakers are turned on ;)

[DllImport("winmm.dll", EntryPoi开发者_开发问答nt="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;
 }
}


I am not really sure about your implementation of winmm.dll but I have a tested and working code for it.

I got the source code from this open source project: Tea Timer.

The implementation of the code is pretty straight forward as below. Hope it helps.

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: This is how you play and stop a music file:

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


I used to use the definition...

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

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

...in .Net 3.5 but in .Net 4.0 is gave me and unbalanced pinvoke exception! I fixed it by using this instead...

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

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

...and passing in IntPtr.Zero as the last param.

The only difference is the uReturnLength is an int (and not a long) and hwndCallback is a IntPtr (and not a long).

Hope this helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜