MIDI Listener on Android with Jetpack Compose and Kotlin
It has been 10 years since I wrote my first mobile app. Back then, I acquired a new MacBook and a MIDI piano that I wanted to analyze. So, I started coding and published the results in the Apple AppStore.
Later, I also released the fundamental source code for receiving and interpreting MIDI events on Apple devices, and wrote an article about it.
As time went by, I started developing mobile apps for Android devices. As a small practice project, I wrote a similar application. I chose to use the latest technology and implemented the project with Jetpack Compose in Kotlin.
The result of this effort is the following blog post and accompanying source code, which can be downloaded from here or by clicking on the image below.
You can see the application in action below. I connected it to my Arturia MiniLab3 via USB-C.
Prerequisites #
There are a couple of things that are needed to build and run the code.
- Android Device
- MIDI Device
- Cable to link both devices
Code Walkthrough #
Below is a summary of the responsibilities of the classes I have written for this application.
MainActivity
: This is the main entry point to the application. It gets created with every new Jetpack Compose project by Android Studio.MyMidiReceiver
: The main action happens in this class. It receives the bytes, interprets the incoming bytes, and converts them to our own MIDI message format.MidiConstants
: We need a couple of constants to interpret the byte-format of the MIDI messages and their length.MidiEvent
: This is the data object for a MIDI event.MidiEventList
: This is the view that displays all the received MIDI messages.MidiViewModel
: This is the view model that acts as an intermediary between the view and the receiver for the MIDI events.
We also need to declare, that our app uses the MIDI-feature. We do this by adding the following line in the AndroidManifest.xml
.
<uses-feature android:name="android.software.midi" android:required="true"/>
Receiving MIDI Messages #
To receive MIDI messages on an Android device, the MIDI device must be connected to the Android device via a cable. Typically, this is done with a USB Type X to USB Type Y cable. In my case, I use a USB-C to USB-C cable with an adapter.
If your MIDI device has a standard MIDI-Out port, you can use an adapter like the Roland UM-ONE mk2.
Bluetooth is also an option for some devices, but I have not tested it with this code yet. I will leave that for another time.
When everything is connected properly and you hit the keys or pads of your MIDI device, the corresponding MIDI message should be displayed on your Android device.
Conclusion #
You can use the provided source code to display and analyze MIDI messages from your MIDI device on your Android phone or tablet. It can be easily extended to meet your specific needs. You can download it from here
If you have any questions, suggestions, or need help implementing a specific use-case, feel free to leave a comment or contact me directly.
Thank you for reading!
- If you enjoyed this, please follow me on Medium
- Buy me a coffee or send me a tip
- Support me and other Medium writers by signing up here
https://twissmueller.medium.com/membership