Skip to content

MIDI event handling with AbstractSingleMidiActivity

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

With extending the AbstractSingleMidiActivity, a single USB MIDI device can be connected with the Activity.

Device Connection

Android [USB A port / microUSB port with USB OTG cable]--- USB MIDI Device

MIDI event receiving

  • Implement the MIDI event handling method (named "onMidi...") to receive MIDI events.
  • 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 AbstractSingleMidiActivity {

    // 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 AbstractSingleMidiActivity's getMidiOutputDevices() method to get the instance of MIDIOutputDevice.
    • And call the instance's method (named "sendMidi...") to send MIDI events.
      • NOTE: The first found USB MIDI device will be detected if multiple devices has been attached.
      • NOTE: If output endpoint doesn't connected, getMidiOutputDevices() method returns null.