从内存中用 C# 播放 MIDI 文件

发布于 2024-11-08 20:52:35 字数 1436 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过潦 2024-11-15 20:52:35

好吧,我做了一个临时解决方案。
仍然有效

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);
        }

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);
        }
御守 2024-11-15 20:52:35

您可以使用 SoundPlayer 类:

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

You can use the SoundPlayer class:

using (Stream midi = Resources.ResourceManager.GetStream("myMidi"))
{
    using (SoundPlayer player = new SoundPlayer(midi))
    {
        player.Play();
    }
}
少钕鈤記 2024-11-15 20:52:35

您可以使用具有播放功能的.NET 库。 DryWetMIDI 允许这样做(示例显示通过默认 Windows 合成器播放 MIDI 文件):

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 数据: 播放

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文