简体   繁体   中英

Working with MIDI in Windows Store App (Win 8.1)

My goal is to receive MIDI messages in Windows Store Apps. Microsoft has delivered an API called Microsoft.WindowsPreview.MidiRT (as a nuget package).

I managed to get a midi port, but MessageReceived event is not arised, although I'm pressing keys on my MIDI keyboard, and other MIDI programs show me that PC receives these messages.

Here is my code:

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

Any ideas?

Possibly-related: Your device watcher code is not following the normal pattern. Here is what you need to do:

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

I managed to make it work. I've changed the platfrom to x64, and now it works (I used to build it for x86). There is still a problem though (and it is even bigger): I want to integrate this with Unity3d, but Unity3d doesn't allow to build x64 windows apps, on the other hand x86 MIDI build doesn't work on x64 machines.

Added:

Although this API depends on your architecture, a new Windows 10 api reportedly does not, so it should be simpler, if you target Win10.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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