我的目标是在Windows Store Apps中接收MIDI消息.Microsoft提供了一个称为Microsoft.WindowsPreview.MidiRT的API(作为nuget包).我设法获得了一个Midi端口,但是没有出现MessageReceived事件,尽管我在MIDI键盘上按了键...

我的目标是在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();
}
织梦狗教程
本文标题为:c#-在Windows Store应用中使用MIDI(Win 8.1)


基础教程推荐
猜你喜欢
- C#中char和string的入门使用教程 2023-03-08
- Unity实现3D射箭小游戏 2023-04-14
- c#之事件用法 2023-05-30
- Unity3D利用DoTween实现卡牌翻转效果 2023-01-16
- C#如何连接使用Zookeeper 2023-04-21
- 如何保存Unity中的Log日志 2023-04-09
- C# DateTime日期比较方法案例详解 2023-04-27
- C#调用OpenCV开发简易版美图工具【推荐】 2023-02-02
- C#使用随机数编写班级点名器的示例代码 2023-06-05
- C#类型转换之自定义隐式转换和显式转换 2023-05-26