自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
MIDI
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
樂器數位介面 (MIDI) 是一種標準通訊協定,可將電腦與樂器、舞台燈光和其他以時間為導向的媒體連結。
自 Android 13 起,USB 傳輸功能已新增 MIDI 2.0 支援功能。MIDI 2.0 是 2020 年定義的新 MIDI 標準。這個通訊協定新增了雙向通訊、更高解析度和資源交換等功能。在平台上啟用 MIDI 支援功能時,一律會啟用 USB 主機模式的 MIDI 2.0。
嚴格來說,MIDI 與音訊無關。不過,MIDI 通常用於音樂,因此本文會放在音訊部分。
NDK 適用的 MIDI
使用 Android 10,您可以更輕鬆地將使用 MIDI 的專業音訊應用程式移植至 Android 平台。
AMidi
是 NDK API,可讓應用程式開發人員透過 C/C++ 程式碼傳送及接收 MIDI 資料。
Android MIDI 應用程式通常會使用
midi
API 與 Android MIDI 服務通訊。MIDI 應用程式主要仰賴
MidiManager
類別來尋找、開啟及關閉一或多個
MidiDevice
物件,並透過裝置的 MIDI 輸入和輸出通訊埠,在裝置間傳遞資料。
傳輸
在原始 MIDI 1.0 中指定的實體傳輸層,是使用 5 針 DIN 連接器的電流迴路。
自 MIDI 1.0 以來,已定義其他傳輸方式,包括 MIDI over USB 和 MIDI over 藍牙低功耗 (BLE)。
Android 版 MIDI
Android 支援 USB On-The-Go,可讓 Android 裝置充當 USB 主機,驅動 USB 外接裝置。USB 主機模式 API 可讓開發人員在應用程式層級實作 MIDI 和 USB,但直到最近,MIDI 才有內建的平台 API。
自 Android 6.0 (Marshmallow) 版本起,裝置製造商可以在平台中啟用選用的 MIDI 支援功能。Android 直接支援 USB、BLE 和虛擬 (跨應用程式) 傳輸。Android 可透過外部轉接器支援 MIDI 1.0。
如要進一步瞭解如何使用 MIDI API 進行應用程式程式設計,請參閱 android.media.midi
套件。
本文其餘部分將討論 Android 裝置製造商如何在平台中啟用 MIDI 支援功能。
啟用 MIDI 1.0 傳輸
MIDI 1.0 實作方式取決於 ALSA 的 USB 主機模式和 USB 周邊裝置模式傳輸。ALSA 不適用於 BLE 和虛擬傳輸。
USB 主機模式
如要為 USB 主機模式啟用 MIDI,請先一般支援 USB 主機模式,然後在核心設定中啟用 CONFIG_SND_RAWMIDI
和 CONFIG_SND_USB_MIDI
。
MIDI 裝置的通用串列匯流排裝置類別定義 (1999 年 11 月 1 日發布第 1.0 版)標準由 USB 實作者論壇公司發布,正式定義 MIDI 透過 USB 傳輸。
USB 周邊裝置模式
如要啟用 USB 周邊模式的 MIDI,您可能需要為 Linux 核心套用修正程式,將 drivers/usb/gadget/f_midi.c
整合至 USB 小工具驅動程式。這些修補程式適用於 Linux kernel 3.10 版。這些修正尚未針對 ConfigFs (USB 小工具驅動程式的新架構) 進行更新,也未在上游 kernel.org 中合併。
以下是專案 kernel/common
分支版本 android-3.10
中,核心樹狀結構的修補程式順序:
- https://android-review.googlesource.com/#/c/127450/
- https://android-review.googlesource.com/#/c/127452/
- https://android-review.googlesource.com/#/c/143714/
您也必須採取下列任一做法:
- 依序前往「設定」>「開發人員選項」>「網路」,然後在「Select USB Configuration」對話方塊中勾選 MIDI 的方塊。
- 連接至 USB 主機時,請從螢幕頂端向下滑動,選取「USB for」項目,然後選取「MIDI」。
BLE
只要裝置支援 BLE,就會一律啟用 MIDI over BLE。
虛擬 (跨應用程式)
虛擬 (跨應用程式) 傳輸功能一律會啟用。
聲明擁有權
應用程式可以使用 android.software.midi
功能,篩選 MIDI 支援功能是否存在。
如要聲明 MIDI 支援功能,請在 device.mk
中新增這一行:
PRODUCT_COPY_FILES += \
frameworks/native/data/etc/android.software.midi.xml:system/etc/permissions/android.software.
midi.xml
如要瞭解聲明功能的相關要求,請參閱 Android 相容性定義說明文件 (CDD)。
在主機模式下進行偵錯
在 USB 主機模式下,無法透過 USB 進行 Android Debug Bridge (adb) 偵錯。如需替代方案,請參閱 Android Debug Bridge 的「
無線使用」一節。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-06-12 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-06-12 (世界標準時間)。"],[],[],null,["# MIDI\n\n[Musical Instrument Digital\nInterface (MIDI)](http://en.wikipedia.org/wiki/MIDI) is a standard protocol for interconnecting computers with musical\ninstruments, stage lighting, and other time-oriented media.\n\n\nStarting in Android 13, [MIDI 2.0](https://www.midi.org/midi-articles/details-about-midi-2-0-midi-ci-profiles-and-property-exchange) support is added to the USB transport. MIDI 2.0 is a newer MIDI\nstandard defined in 2020. This protocol adds features such as two-way communication, higher\nresolution and property exchange. MIDI 2.0 through USB host mode is always enabled when MIDI support\nis enabled on the platform.\n\n\nStrictly speaking, MIDI is unrelated to audio. But because MIDI is commonly used with\nmusic, this article is placed in the audio section.\n\nMIDI for NDK\n------------\n\nAndroid 10 makes it easier to port professional audio apps\nusing MIDI to the Android platform.\n\n[AMidi](https://developer.android.com/ndk/reference/group/midi) is an\n[NDK API](https://developer.android.com/ndk) that gives app\ndevelopers the ability to send and receive MIDI data with C/C++ code.\n\nAndroid MIDI apps usually use the [midi](https://developer.android.com/reference/android/media/midi/package-summary) API to communicate with the Android MIDI service. MIDI\napps primarily depend on the [MidiManager](https://developer.android.com/reference/android/media/midi/MidiManager) class to discover, open, and close one or more [MidiDevice](https://developer.android.com/reference/android/media/midi/MidiDevice)\nobjects, and pass data to and from each device through the device's MIDI input and output ports.\n\nTransports\n----------\n\n\nThe physical [transport\nlayer](http://en.wikipedia.org/wiki/Transport_layer)\nspecified in original MIDI 1.0 is a current loop with\n[5-pin DIN](http://en.wikipedia.org/wiki/DIN_connector) connector.\n\n\nSince MIDI 1.0, additional transports have been defined, including MIDI over USB\nand MIDI over\n[Bluetooth Low Energy\n(BLE)](http://en.wikipedia.org/wiki/Bluetooth_low_energy).\n\nMIDI for Android\n----------------\n\n\nAndroid supports\n[USB On-The-Go](http://en.wikipedia.org/wiki/USB_On-The-Go),\nwhich permits an Android device to act as a USB host to drive USB\nperipherals. The USB host mode APIs permit\ndevelopers to implement MIDI over USB at the application level, but until\nrecently there have been no built-in platform APIs for MIDI.\n\n\nBeginning with the Android 6.0 (Marshmallow) release, device makers can enable optional MIDI\nsupport in the platform. Android directly supports USB, BLE, and virtual (interapp) transports.\nAndroid supports MIDI 1.0 through an external adapter.\n\n\nFor details on application programming with the MIDI APIs, see the\n[`android.media.midi`](https://developer.android.com/reference/android/media/midi/package-summary.html)\npackage.\n\n\nThe remainder of this article discusses how an Android device maker can\nenable MIDI support in the platform.\n\nEnabling MIDI 1.0 transports\n----------------------------\n\n\nMIDI 1.0 implementation depends on ALSA for USB host mode and USB peripheral mode transports.\nALSA isn't used for the BLE and virtual transports.\n\n### USB host mode\n\n\nTo enable MIDI for USB host mode, first support USB host mode in general, and\nthen enable `CONFIG_SND_RAWMIDI` and `CONFIG_SND_USB_MIDI` in your kernel\nconfiguration.\n\n\nThe MIDI over USB transport is formally defined by the\n[Universal Serial Bus Device Class Definition for MIDI Devices Release 1.0 Nov 1, 1999](https://www.usb.org/sites/default/files/midi10.pdf)\nstandard published by the\n[USB Implementers Forum, Inc](http://www.usb.org/).\n\n### USB peripheral mode\n\n\nTo enable MIDI for USB peripheral mode, you may need to apply patches\nto your Linux kernel to integrate `drivers/usb/gadget/f_midi.c` into the USB gadget\ndriver. These patches are available for Linux kernel version\n3.10. These patches have not yet been updated for\n[ConfigFs](http://en.wikipedia.org/wiki/Configfs)\n(a new architecture\nfor USB gadget drivers), nor are they merged at upstream\n[kernel.org](http://kernel.org).\n\n\nThe patches are shown in commit order for the kernel tree at project `kernel/common`\nbranch `android-3.10`:\n\n1. \u003chttps://android-review.googlesource.com/#/c/127450/\u003e\n2. \u003chttps://android-review.googlesource.com/#/c/127452/\u003e\n3. \u003chttps://android-review.googlesource.com/#/c/143714/\u003e\n\nYou also need to do either of the following:\n\n- Go to **Settings \\\u003e Developer options \\\u003e Networking** and check the box for MIDI in the Select USB Configuration dialog.\n- While attached to the USB host, pull down from the top of screen, select the entry **USB for** , and then select **MIDI**.\n\n### BLE\n\n\nMIDI over BLE is always enabled, provided the device supports BLE.\n\n### Virtual (interapp)\n\n\nThe virtual (interapp) transport is always enabled.\n\nClaiming the feature\n--------------------\n\n\nApps can screen for the presence of MIDI support using the\n`android.software.midi` feature.\n\n\nTo claim MIDI support, add this line to your `device.mk`: \n\n```\nPRODUCT_COPY_FILES += \\\nframeworks/native/data/etc/android.software.midi.xml:system/etc/permissions/android.software.\nmidi.xml\n```\n\n\nSee the\n[Android Compatibility Definition Document (CDD)](/static/docs/compatibility/android-cdd.pdf)\nfor information\non requirements to claim the feature.\n\nDebugging while in host mode\n----------------------------\n\n\nWhile in USB host mode, Android Debug Bridge (adb) debugging over USB is unavailable.\nSee the [Wireless usage](http://developer.android.com/tools/help/adb.html#wireless) section of Android Debug Bridge for an alternative."]]