Playing a MIDI file in C # from memory

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

+3
source share
3 answers

Ok, I made a temporary fix. However, 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);
        }
+1
source

You can use the SoundPlayer class :

using (Stream midi = Resources.ResourceManager.GetStream("myMidi"))
{
    using (SoundPlayer player = new SoundPlayer(midi))
    {
        player.Play();
    }
}
0
source

.NET, . DryWetMIDI ( MIDI Windows ):

var midiFile = MidiFile.Read("Greatest song ever.mid");

// Or you can read a MIDI file from a stream
// var midiFile = MidiFile.Read(stream);

using (var outputDevice = OutputDevice.GetByName("Microsoft GS Wavetable Synth"))
{
    midiFile.Play(outputDevice);
}

MIDI- Wiki: Playback.

0

All Articles