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

时间:2015-05-31 17:18:36

标签: c# audio windows-store-apps midi

我的目标是在Windows应用商店应用中接收MIDI信息。 Microsoft提供了一个名为Microsoft.WindowsPreview.MidiRT的API(作为nuget包)。

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

这是我的代码:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

可能相关:您的设备观察程序代码未遵循正常模式。以下是您需要做的事情:

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

答案 1 :(得分:1)

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

<强>加了:

虽然这个API取决于你的架构,但据报道新的Windows 10 api没有,所以如果你的目标是Win10,它会更简单。