簡體   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