繁体   English   中英

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

[英]Working with MIDI in Windows Store App (Win 8.1)

我的目标是在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);
    }
}

有任何想法吗?

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

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,它应该更简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM