Playing MIDI file in C# from memory [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this questionHow 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
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);
}
You can use the SoundPlayer class:
using (Stream midi = Resources.ResourceManager.GetStream("myMidi"))
{
using (SoundPlayer player = new SoundPlayer(midi))
{
player.Play();
}
}
You can use a .NET library that has playback functionality. DryWetMIDI allows this (example shows playing a MIDI file via default Windows synthesizer):
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);
}
You can read more about playing MIDI data on the library docs: Playback.
精彩评论