在 Windows Store App (Win 8.1) 中使用 MIDI
Working with MIDI in Windows Store App (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);
}
}
有什么想法吗?
我成功了。我已经将平台更改为 x64,现在它可以工作了(我曾经为 x86 构建它)。但仍然存在问题(甚至更大):我想将它与 Unity3d 集成,但 Unity3d 不允许构建 x64 windows 应用程序,另一方面 x86 MIDI 构建不起作用x64 机器。
已添加:
虽然这个 API 取决于您的体系结构,但据报道新的 Windows 10 api 不会,因此如果您的目标是 Win10,它应该更简单。
可能相关:您的设备观察程序代码未遵循正常模式。这是您需要做的:
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 消息。
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);
}
}
有什么想法吗?
我成功了。我已经将平台更改为 x64,现在它可以工作了(我曾经为 x86 构建它)。但仍然存在问题(甚至更大):我想将它与 Unity3d 集成,但 Unity3d 不允许构建 x64 windows 应用程序,另一方面 x86 MIDI 构建不起作用x64 机器。
已添加:
虽然这个 API 取决于您的体系结构,但据报道新的 Windows 10 api 不会,因此如果您的目标是 Win10,它应该更简单。
可能相关:您的设备观察程序代码未遵循正常模式。这是您需要做的:
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();
}