Documentation
¶
Overview ¶
MIDI API overview and reference example.
Demonstrates the full device-to-port lifecycle of the ndk/midi package, which wraps Android's native MIDI API (AMidi). The example shows how to:
- Obtain an AMidiDevice handle from the Java MidiManager via JNI
- Inspect the number of input and output ports on the device
- Open an input port and send MIDI messages (Note On, Note Off, Control Change, Program Change)
- Construct MIDI messages as byte arrays with the correct status bytes
- Close ports and the device in the correct order
Obtaining the AMidiDevice handle ¶
The Android NDK does not provide a way to discover or open MIDI devices from C/native code. Device discovery happens on the Java side through android.media.midi.MidiManager. A typical pattern is:
- In Java/Kotlin, use MidiManager.openDevice() to get a MidiDevice.
- Call AMidiDevice_fromJava(env, javaMidiDevice) via JNI to obtain the native AMidiDevice pointer.
- Pass that pointer to Go (e.g. via a cgo bridge or as an unsafe.Pointer stored in a global).
Because this example cannot run standalone (it requires a live Android MIDI device obtained through JNI), it prints reference information about MIDI message formats and demonstrates the API calls that would be made once a Device handle is available.
This program must run on an Android device.
Click to show internal directories.
Click to hide internal directories.