Ed Poor
Ed Poor

Reputation: 1879

PackageManager says device doesn't have FEATURE_MIDI

How can I write an Android app to receive MIDI events from my Yamaha YDP-113 electronic piano?

When I force it, I get a runtime error:

FATAL EXCEPTION: main
Process: com.example.android.miditest, PID: 19022
java.lang.NoClassDefFoundError: android.media.midi.MidiManager

The hasSystemFeature() method returns false:

    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI)) {
        // do MIDI stuff
        Log.d("MainActivity-onCreate", "has MIDI feature");

        TextView info = (TextView) findViewById(R.id.info_text_view);
        info.setText("MIDI found!");

        MidiManager m = (MidiManager) getSystemService(Context.MIDI_SERVICE);
    }
    else {
        Log.d("MainActivity-onCreate", "cannot find MIDI feature");

        TextView info = (TextView) findViewById(R.id.info_text_view);
        info.setText("Sorry, no MIDI");
    }

Here is my manifest:

<uses-feature android:name="android.software.midi" android:required="true"/>
<uses-feature android:name="android.hardware.usb.host"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I am sure it must be possible, because I installed "USB Midi Keyboard" by AppsTransport, which has 100,000 downloads on Google Play Store. When I play notes on my Yamaha YDP-113, the app shows which piano keyboard key I pressed, and even makes the sound!

What are the other developers using, which allows them to make apps that work with my keyboard, my MIDI-USB cable, and my Samsung tablet? Is there something I need to install on my phone? Or some package or download I need to tell gradle about?

Upvotes: 1

Views: 489

Answers (1)

Eugen Pechanec
Eugen Pechanec

Reputation: 38243

MidiManager is only available on Android 6+. The class and associated functionality doesn't exist on previous releases hence the error if you run the app on older devices.

If you want to support older devices than Android 6, you're going to have to use some sort of proprietary SDK or third party library.

A quick web search suggests kshoji/USB-MIDI-Driver may help you.

Please note that if you specify

<uses-feature android:name="android.software.midi" android:required="true"/>

the app will not appear in the Play Store for old devices that do not support the MIDI API.

Source: https://developer.android.com/reference/android/media/midi/package-summary.html

You might want to remove that line.

Upvotes: 1

Related Questions