Search code examples
c#winapimidi

Playing MIDI file in C# from memory


How can I play the MIDI file directly from memory (e.g. Resources)? I found this as a solution, but I do not understand how to play the MIDI from resources. Help me with the right WinAPI command please


Solution

  • Ok, i made kind of a temporary solution. Still, it works

    using (var midiStream = new MemoryStream(Resources.myMidi))
            {
                var data = midiStream.ToArray();
                try
                {
                    using (var fs = new FileStream("midi.mid", FileMode.CreateNew, FileAccess.Write))
                    {
                        fs.Write(data, 0, data.Length);
                    } 
                }
                catch(IOException)
                {}
                string sCommand = "open \"" + Application.StartupPath + "/midi.mid" + "\" alias " + "MIDIapp";
                mciSendString(sCommand, null, 0, IntPtr.Zero);
                sCommand = "play " + "MIDIapp";
                mciSendString(sCommand, null, 0, IntPtr.Zero);
            }