Share via


MidiOutPort.FromIdAsync(String) Method

Definition

Creates a MidiOutPort object for the specified device.

public:
 static IAsyncOperation<IMidiOutPort ^> ^ FromIdAsync(Platform::String ^ deviceId);
/// [Windows.Foundation.Metadata.RemoteAsync]
 static IAsyncOperation<IMidiOutPort> FromIdAsync(winrt::hstring const& deviceId);
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<IMidiOutPort> FromIdAsync(string deviceId);
function fromIdAsync(deviceId)
Public Shared Function FromIdAsync (deviceId As String) As IAsyncOperation(Of IMidiOutPort)

Parameters

deviceId
String

Platform::String

winrt::hstring

The device ID, which can be obtained by enumerating the devices on the system Windows.Devices.Enumeration.DeviceInformation.FindAllAsync.

Returns

The asynchronous operation. Upon completion, IAsyncOperation.GetResults returns a MidiOutPort object.

Attributes

Examples

// Opens the default MIDI output device.
private async Task<IMidiOutPort> OpenDefaultMidiOut()
{
    IMidiOutPort midiOut = null;

    string midiOutQueryString = MidiOutPort.GetDeviceSelector();
    DeviceInformationCollection midiOutDevices = await DeviceInformation.FindAllAsync(midiOutQueryString);
    int selectedMidiDevice = 0;

    if (0 == midiOutDevices.Count)
    {
        Debug.WriteLine("No Midi output devices");
        return null;
    }
    // If there are > 1 MIDI out devices, pick the first non-integrated one.
    else if (2 <= midiOutDevices.Count)
    {
        for (int midiDeviceIdx = 0; midiDeviceIdx < midiOutDevices.Count; midiDeviceIdx++)
        {
            if (false == MidiSynthesizer.IsSynthesizer(midiOutDevices[midiDeviceIdx]))
            {
                selectedMidiDevice = midiDeviceIdx;
                break;
            }
        }
    }

    midiOut = await MidiOutPort.FromIdAsync(midiOutDevices[selectedMidiDevice].Id);

    return midiOut;
}
// Opens the default MIDI output device.
winrt::Windows::Foundation::IAsyncOperation<IMidiOutPort> OpenDefaultMidiOut()
{
    IMidiOutPort midiOut{ nullptr };

    winrt::hstring midiOutQueryString{ MidiOutPort::GetDeviceSelector() };
    DeviceInformationCollection midiOutDevices{ co_await DeviceInformation::FindAllAsync(midiOutQueryString) };
    uint32_t selectedMidiDevice{ 0 };

    if (0 == midiOutDevices.Size())
    {
        // No Midi output devices.
        co_return midiOut;
    }
    // If there are > 1 MIDI out devices, pick the first non-integrated one.
    else if (2 <= midiOutDevices.Size())
    {
        for (uint32_t midiDeviceIdx = 0; midiDeviceIdx < midiOutDevices.Size(); midiDeviceIdx++)
        {
            if (false == MidiSynthesizer::IsSynthesizer(midiOutDevices.GetAt(midiDeviceIdx)))
            {
                selectedMidiDevice = midiDeviceIdx;
                break;
            }
        }
    }

    midiOut = co_await MidiOutPort::FromIdAsync(midiOutDevices.GetAt(selectedMidiDevice).Id());

    co_return midiOut;
}

Remarks

To enumerate the MidiOutPort objects on the system, pass the query string provided by GetDeviceSelector to Windows.Devices.Enumeration.DeviceInformation.FindAllAsync.

Suggested times to attempt to create a MidiInPort are on any sort of app activation or user interaction. If a function call on a MidiInPort fails, or if a message received from a MidiInPort is invalid, do not try to recreate the port immediately.

Applies to