系统相关
首页 > 系统相关> > c#-在Windows Store应用中使用MIDI(Win 8.1)

c#-在Windows Store应用中使用MIDI(Win 8.1)

作者:互联网

我的目标是在Windows Store Apps中接收MIDI消息.
Microsoft提供了一个称为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);
    }
}

有任何想法吗?

解决方法:

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

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

标签:windows-store-apps,midi,audio,c
来源: https://codeday.me/bug/20191120/2042387.html