Search code examples
androidandroid-mediaplayer

Obtaining a list of MediaBrowserServices currently registered results in only the bluetooth MediaBrowserService


I'm trying to build an app where one can start audio playback from WearOS. From what I can tell this requires an app on the phone to obtain a list of possible songs to be played. To do that I want to use the MediaBrowserService, like this example does.

However when I try to reproduce the listing of MediaBrowserServices, I only get the "Bluetooth audio" MediaBrowserService. The example app on the other hand works fine and displays all apps.

Here is the code I'm using:

override fun onStart() {
    super.onStart()
    val mediaBrowserIntent = Intent(MediaBrowserServiceCompat.SERVICE_INTERFACE)

    val packageManager = this.packageManager
    val services = packageManager.queryIntentServices(mediaBrowserIntent, PackageManager.GET_RESOLVED_FILTER)
    val tv: TextView = findViewById(R.id.helloworld)
    val res = StringBuilder()
    for(info in services) {
        res.appendLine(info.serviceInfo.loadLabel(packageManager).toString())
    }
    tv.text = res.toString()
}

This is adapted from here.

Do you know what might be causing the issue?


Solution

  • It looks like you need to properly handle the package visibility.
    Try to add <queries> section to your manifest file with the correct intent filter signature:

    <queries>
        <intent>
          <action android:name="android.media.browse.MediaBrowserService" />
        </intent>
    </queries>
    

    The sample app works because it targets SDK 29.