支援語音動作
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
將語音指令整合至媒體應用程式,盡可能減少干擾元素,提升駕駛安全和便利性。Android Auto 或 Android Automotive OS (AAOS) 偵測及解讀語音指令時,會透過 onPlayFromSearch
回呼將指令傳送至應用程式。
收到回呼後,應用程式會尋找與查詢字串相符的內容,然後開始播放。應用程式必須支援各種查詢類別,例如類型、藝人、專輯、歌曲名稱、電台或播放清單。此外,應用程式必須處理空白的查詢字串,這表示一般音樂要求。
如果應用程式正在播放某個媒體項目,使用者可能會說「播放 [曲目標題]」來指示應用程式播放其他歌曲,不必查看或輕觸車輛的螢幕。使用者只要在方向盤上按下適當的按鈕,或說出啟動字詞「Ok Google」,即可啟動查詢。
Android Auto 或 Android Automotive OS 偵測及解讀語音指令時,該語音指令會透過 onPlayFromSearch
傳送至應用程式。收到此回呼後,應用程式會尋找與 query
字串相符的內容,然後開始播放。
使用者可以在查詢中指定不同字詞的類別,包括類型、演出者、專輯、歌曲名稱、廣播電台或播放清單等等。建構搜尋的支援功能時,請將與您應用程式相關的所有類別納入考量。如果 Android Auto 或 AAOS 偵測到特定查詢符合某個類別,就會在 extras
參數附加額外項目。你可以傳送下列額外資訊:
計算空白 query
字串,如果使用者未指定搜尋字詞,可由 Android Auto 或 AAOS 傳送該字串。例如,使用者說「播放音樂」。在這種情況下,應用程式可以啟動最近播放或新的曲目。
如果應用程式無法快速處理搜尋,請不要卡在 onPlayFromSearch
中。請將播放狀態設為 STATE_CONNECTING
,然後在非同步執行緒上執行搜尋。
開始播放後,請考慮在媒體工作階段的待播清單中填入相關內容。舉例來說,如果使用者要求播放專輯,您的應用程式便可在待播清單中填入專輯的曲目清單。
除了「播放」查詢,Android Auto 和 AAOS 也會辨識控制播放的語音查詢 (例如「暫停播放音樂」和「下一首歌曲」),並將這些指令與 onPause
和 onSkipToNext
等適當的媒體工作階段回呼進行比對。
如要進一步瞭解如何實作支援語音啟用播放功能,請參閱「Google 助理和媒體應用程式」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-22 (世界標準時間)。
[null,null,["上次更新時間:2025-08-22 (世界標準時間)。"],[],[],null,["# Support voice actions\n\nIntegrate voice actions into your media app to enhance driver safety and\nconvenience by minimizing distractions. When Android Auto or Android Automotive\nOS (AAOS) detects and interprets a voice action, they deliver the action to your\napp through the `onPlayFromSearch` callback.\n\nUpon receiving the callback, your app finds content that matches the query\nstring and then starts playback. Your app must account for various query\ncategories, such as genre, artist, album, song name, radio station, or playlist.\nAdditionally, your app must handle an empty query string, which indicates a\ngeneral request for music.\n\nIf your app is playing one media item, the user can say \"Play \\[song title\\]\"\nto tell your app to play a different song without looking at or touching the\ncar's display. Users can initiate queries by clicking the appropriate buttons\non their steering wheel or speaking the hotwords \"OK Google.\"\n| **Design guidelines:** To learn more, see [Create apps](/cars/design/create-apps).\n\nWhen Android Auto or AAOS detects and interprets a voice action, Android Auto or\nAAOS delivers that voice action to the app through [`onPlayFromSearch`](/reference/android/support/v4/media/session/MediaSessionCompat.Callback#onPlayFromSearch(java.lang.String,%20android.os.Bundle)). Upon\nreceiving this callback, the app finds content to match the `query` string and\nthen starts playback.\n\nUsers can specify different categories of terms in their query: genre, artist,\nalbum, song name, radio station, or playlist, among others. When building\nsupport for search, account for all the categories that make sense for your app.\nIf Android Auto or AAOS detects that a given query fits into a specific\ncategory, extras are appended in the `extras` parameter. You can send these\nextras:\n\n- [`EXTRA_MEDIA_ALBUM`](/reference/android/provider/MediaStore#EXTRA_MEDIA_ALBUM)\n- [`EXTRA_MEDIA_ARTIST`](/reference/android/provider/MediaStore#EXTRA_MEDIA_ARTIST)\n- [`EXTRA_MEDIA_GENRE`](/reference/android/provider/MediaStore#EXTRA_MEDIA_GENRE)\n- [`EXTRA_MEDIA_PLAYLIST`](/reference/android/provider/MediaStore#EXTRA_MEDIA_PLAYLIST)\n- [`EXTRA_MEDIA_TITLE`](/reference/android/provider/MediaStore#EXTRA_MEDIA_TITLE)\n\nAccount for an empty `query` string, which can be sent by Android Auto or AAOS\nif the user doesn't specify search terms. For example, if the user says\n\"Play some music.\" In this case, your app can start a recently played or new\ntrack.\n\nIf your app can't process a search quickly, don't block in `onPlayFromSearch`.\nInstead, set the playback state to [`STATE_CONNECTING`](/reference/android/support/v4/media/session/PlaybackStateCompat#STATE_CONNECTING()) and perform the\nsearch on an async thread.\n\nWhen playback starts, consider populating the media session's queue with\nrelated content. For example, if the user requests an album to be played, your\napp could fill the queue with the album's tracklist.\n\nIn addition to \"Play\" queries, Android Auto and AAOS recognize voice queries to\ncontrol playback like \"pause music\" and \"next song\" and match these commands to\nthe appropriate media session callbacks, such as `onPause` and `onSkipToNext`.\n\nTo learn more about implementing voice-enabled playback actions, see\n[Google Assistant and media apps](/media/implement/assistant)."]]