PackageManager 说设备没有 FEATURE_MIDI

PackageManager says device doesn't have FEATURE_MIDI

如何编写 Android 应用程序来接收来自我的 Yamaha YDP-113 电子钢琴的 MIDI 事件?

当我强制执行时,出现运行时错误:

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

hasSystemFeature() 方法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");
    }

这是我的清单:

<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>

我确信这一定是可能的,因为我通过 AppsTransport 安装了 "USB Midi Keyboard",它在 Google Play 商店中有 100,000 次下载。当我在 Yamaha YDP-113 上弹奏音符时,应用程序会显示我按下的是哪个钢琴键盘琴键,甚至会发出声音!

其他开发人员使用什么,使他们能够制作适用于我的键盘、我的 MIDI-USB 电缆和我的三星平板电脑的应用程序?我需要在 phone 上安装什么东西吗?或者我需要告诉 gradle 一些包或下载?

MidiManager 仅适用于 Android 6+。 class 和相关功能 在以前的版本中不存在,因此如果您 运行 旧设备上的应用程序会出现错误。

如果您想支持比 Android 6 更旧的设备,您将不得不使用某种专有 SDK 或第三方库。

快速网络搜索表明 kshoji/USB-MIDI-Driver 可能对您有所帮助。

请注意,如果您指定

<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.

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

您可能想要删除该行。