Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3221258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:51:20+00:00 2026-05-17T15:51:20+00:00

I use this code to play a MIDI file for my game, but I

  • 0

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", 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;
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:51:21+00:00Added an answer on May 17, 2026 at 3:51 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I use this way to play soundeffect SoundEffect _effect; using (var stream =
I want to set alrms so i use this code. UILocalNotification *localNotif = [[UILocalNotification
I have some Android code to stream an audio file from the internet and
I have some Android code to stream an audio file from the internet and
When I run java -jar MidiTest.jar , input a MIDI file, it throws: Exception
What I think I need to do is create some code that lets me
I am currently reorganizing a Play! project where there is a lot of JS
I have a shutdown function that checks to see if a redirect was just
I have a shutdown function that checks to see if a redirect was just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.