-
Notifications
You must be signed in to change notification settings - Fork 105
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.
Android [USB A port / microUSB port with USB OTG cable]---(USB Hub)---┬-- USB MIDI Device
├-- USB MIDI Device
└ ...
- 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 theUI thread
), so you must manipulate the Views with theHandler
andCallback
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));
}
- Call the
getMidiOutputDevices()
method to get the instance ofSet<MIDIOutputDevice>
. - Choose an instance from the
Set<MIDIOutputDevice>
.- And call the instance's method (named
"sendMidi..."
) to send MIDI events.
- And call the instance's method (named