0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[備忘録]AndroidAutoのメディアアプリで再生コンテンツの探索が走る

Last updated at Posted at 2025-12-19

AndroidAutoとは

AndroidAutoに対応したカーナビに接続することで、カーナビからアプリを操作することが可能になるというものです。

発生した問題

カーナビに接続した際、MediaBrowserが実装されているアプリは、AndroidAutoアプリから、おすすめ欄に表示されるコンテンツの取得が行われます。
(添付画像はSpotifyのおすすめ表示)
スクリーンショット 2025-12-19 15.46.55.png

コンテンツの取得の動作として以下の手順で探索が行われます。

  1. rootのidがリクエストされる。
  2. 返却されるMediaBrowserCompat.MediaItem()
    1. FLAG_PLAYABLEが指定されている場合、そのコンテンツをおすすめに表示する。
    2. FLAG_BROWSABLEが指定されている場合、そのidの以下にあるコンテンツを取得する。
  3. 2件取得されるまで探索する。

この時コンテンツの取得処理以外の処理が入っている場合、実行されてしまいます。

対応

onGetRootclientPackageNameにAndroidAutoアプリのパッケージが入ってきたか判断することで、おすすめかどうか判断できるため、その場合はおすすめに表示したいFLAG_PLAYABLEMediaItemのみ返却しましょう。

    override fun onGetRoot(
        clientPackageName: String,
        clientUid: Int,
        rootHints: Bundle?
    ): BrowserRoot {
        val isRecommend = clientPackageName == "com.google.android.projection.gearhead"
        val id = if (isRecommend) {
            MediaLibrary.RECOMMEND_ID
        } else {
            MediaLibrary.ROOT_ID
        }
        return BrowserRoot(id, null)
    }

    override fun onLoadChildren(
        parentId: String,
        result: Result<List<MediaBrowserCompat.MediaItem>>
    ) {
        val items = when (parentId) {
            MediaLibrary.ROOT_ID -> MediaLibrary.rootItems()
            MediaLibrary.RECOMMEND_ID -> MediaLibrary.recommendItems()
            else -> MediaLibrary.childrenFor(parentId)
        }
        result.sendResult(items)
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?