-
Notifications
You must be signed in to change notification settings - Fork 105
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.
Android [USB A port / microUSB port with USB OTG cable]--- USB MIDI Device
- Implement the MIDI event handling method (named
"onMidi..."
) to receive MIDI events. - Note: The method
"onMidi..."
will be called by another thread (NOT theUI thread
), so you must manipulate the Views with theHandler
andCallback
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));
}
- Call AbstractSingleMidiActivity's
getMidiOutputDevices()
method to get the instance ofMIDIOutputDevice
.- 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.
- And call the instance's method (named