使用MIDI在Windows商店应用程序(Win 8.1)

本文关键字:Win 应用程序 MIDI Windows 使用 | 更新日期: 2023-09-27 17:52:13

我的目标是在Windows Store应用程序中接收MIDI消息。微软已经交付了一个名为Microsoft.WindowsPreview.MidiRT的API(作为一个nuget包)。

我设法得到一个midi端口,但MessageReceived事件没有出现,虽然我在我的midi键盘上按键,其他midi程序显示我PC接收这些消息。

下面是我的代码:
public sealed partial class MainPage : Page
{
    private MidiInPort port;
    public MainPage()
    {
        this.InitializeComponent();
        DeviceWatcher watcher = DeviceInformation.CreateWatcher();
        watcher.Updated += watcher_Updated;
        watcher.Start();
    }
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        port.Dispose();
    }
    async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
    {
        DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(MidiInPort.GetDeviceSelector());
        foreach (var item in deviceCollection)
        {
            Debug.WriteLine(item.Name);
            if (port == null)
            {
                port = await MidiInPort.FromIdAsync(item.Id);
                port.MessageReceived += port_MessageReceived;
            }
        }
    }
    void port_MessageReceived(MidiInPort sender, MidiMessageReceivedEventArgs args)
    {
        Debug.WriteLine(args.Message.Type);
    }
}

任何想法?

使用MIDI在Windows商店应用程序(Win 8.1)

可能相关:您的设备监视程序代码不遵循正常模式。下面是你需要做的:

DeviceWatcher midiWatcher;
void MonitorMidiChanges()
{
  if (midiWatcher != null)
    return;
  var selector = MidiInPort.GetDeviceSelector();
  midiWatcher = DeviceInformation.CreateWatcher(selector);
  midiWatcher.Added += (s, a) => Debug.WriteLine("Midi Port named '{0}' with Id {1} was added", a.Name, a.Id);
  midiWatcher.Updated += (s, a) => Debug.WriteLine("Midi Port with Id {1} was updated", a.Id);
  midiWatcher.Removed += (s, a) => Debug.WriteLine("Midi Port with Id {1} was removed", a.Id);
  midiWatcher.EnumerationCompleted += (s, a) => Debug.WriteLine("Initial enumeration complete; watching for changes...");
  midiWatcher.Start();
}

我设法使它工作。我已经将平台更改为x64,现在它可以工作了(我曾经为x86构建它)。但仍然有一个问题(甚至更大):我想把它与Unity3d集成,但Unity3d不允许构建x64 windows应用程序,另一方面,x86 MIDI构建不能在x64机器上工作。

补充道:

虽然这个API取决于你的体系结构,但据报道,一个新的Windows 10 API不需要,所以如果你的目标是Win10,它应该更简单。