Skip to content

MIDI event handling with AbstractMultipleMidiActivity

Kaoru Shoji edited this page Jul 23, 2014 · 1 revision

With extending the AbstractMultipleMidiActivity, multiple USB MIDI devices can be connected with the Activity.

Device Connection

Android [USB A port / microUSB port with USB OTG cable]---(USB Hub)---┬-- USB MIDI Device
                                                                      ├-- USB MIDI Device 
                                                                      └   ...

MIDI event receiving

  • Implement the MIDI event handling method (named "onMidi...") to receive MIDI events.
    • The event sender object(MIDIInputDevice instance) will be set on the first argument.
  • Note: The method "onMidi..." will be called by another thread (NOT the UI thread), so you must manipulate the Views with the Handler and Callback in the UI thread. Like the code below.
public class SampleActivity extends AbstractMultipleMidiActivity {

    // this field belongs to the UI thread
    final Handler uiThreadEventHandler = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if ("note on".equals(msg.obj)) {
                textView.setText("note on event received.");
            }

            // message handled successfully
            return true;
        }
    });

    // this method will be called from the another thread, so it can't change View's state.
    @Override
    public void onMidiNoteOn(final MidiInputDevice sender, int cable, int channel, int note, int velocity) {
        // Send a message to the UI thread
        String message = "note on";
        uiThreadEventHandler.sendMessage(Message.obtain(uiThreadEventHandler, 0, message));
    }

MIDI event sending

  • Call the getMidiOutputDevices() method to get the instance of Set<MIDIOutputDevice>.
  • Choose an instance from the Set<MIDIOutputDevice>.
    • And call the instance's method (named "sendMidi...") to send MIDI events.