Problem implementing MidiInput::openDevice method correctly

Hello everyone.
I have issues implementing the method stated in the title.

My goal was just to instantiate midi events on my application by selecting any device detected ( inside a combobox)

void MainComponent::comboBoxChanged(juce::ComboBox* comboBox)
{
    if (comboBox == &midiDeviceComboBox)
    {
        const int selectedDeviceIndex = midiDeviceComboBox.getSelectedId() - 1;
        if (selectedDeviceIndex >= 0 && selectedDeviceIndex < juce::MidiInput::getAvailableDevices().size())
        {
            // Handle the selected MIDI device here
            const auto& selectedDeviceName = juce::MidiInput::getAvailableDevices()[selectedDeviceIndex];

            juce::MidiInput* retrievedMidiInput;

            retrievedMidiInput = juce::MidiInput::openDevice(selectedDeviceIndex, this);

            if (retrievedMidiInput != nullptr)
            {
                // Start MIDI input
                retrievedMidiInput->start();
            }
        }
    }
}

After many tries and tweaks, I still get a "No matching function for call to 'openDevice’ " error from it.

Is there something I am still not getting or doing wrong?

juce::MidiInput::openDevice() takes a juce::String as its first argument, however it looks like you’re passing in an int.

Did you mean to pass selectedDeviceName instead of selectedDeviceIndex?

Unfortunately, Same error.
Before it was asking to change type to MidiInputCallback , now asking for juce::MidiDeviceInfo.

What about the second argument, is this a juce::MidiInputCallback*? I.e. does MainComponent inherit from juce::MidiInputCallback?

Hello, yes it does, in the header file. Still trying to find out the problem.

I got it to work. however the openDevice Method is deprecated. Any reason?

Because you are using the overload that takes in an integer index instead of the device ID?

1 Like

Only just noticed you were trying to store the result in a juce::MidiInput*, rather than std::unique_ptr<juce::MidiInput>. I assume that’s what you changed to make it work?

The version taking an int as its first argument is deprecated in favour of using a juce::String to identify the device instead.

1 Like

@xenakios , @ImJimmi
Thank you so much! Got the method to fully work now.