Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaBrowserCompat.Connect() always call onConnectionFailed() callback

I am building a android media player which will play a radio stream from URL. Piece of code are as follows:

public class BaseActivity extends ActionBarCastActivity implements MediaBrowserProvider {
private MediaBrowserCompat mMediaBrowser;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mMediaBrowser = new MediaBrowserCompat(this,
            new ComponentName(this, MusicService.class), mConnectionCallback, null);
}

@Override
protected void onStart() {
    super.onStart();

    mMediaBrowser.connect();
}

private final MediaBrowserCompat.ConnectionCallback mConnectionCallback =
    new MediaBrowserCompat.ConnectionCallback() {
        @Override
        public void onConnected() {
            //setSupportMediaController code
        }

        @Override
        public void onConnectionSuspended() {

        }

        @Override
        public void onConnectionFailed() {
            Log.d(TAG, "onConnectionFailed");
        }
    }; 
}

Here MusicService is the service which extends MediaBrowserServiceCompat and yes, put the following code in the manifest:

<service
android:name=".playback.RadioPlayerService"
android:exported="true"
tools:ignore="ExportedService">
<intent-filter>
    <action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>

After running the app, it always call onConnectionFailed callback method. But found no way to discover actual reason for connection failed. Any idea how to find the error?

like image 809
Ratul Avatar asked Feb 21 '17 05:02

Ratul


2 Answers

Replying to this as I was just running into the same issue where I kept getting the onConnectionFailed result. It turns out that the solution was that I forgot to update the name of the service in my AndroidManifest.xml.

I think the problem may be similar here. You do have a service declared in your manifest, however, the name is set to '.playback.RadioPlayerService' and when creating your MediaBrowser, you're referencing 'MediaService.class'. Try replacing

android:name=".playback.RadioPlayerService"

with

android:name=".MusicService"

or, alternatively, update your instantiation of the MediaBrowser to reference RadioPlayerService.class if that exists.

like image 82
Luke Avatar answered Oct 18 '22 01:10

Luke


Got in a similar situation, where the connection always failed because of below;

2020-01-11 18:17:33.760 10560-10560/com.demo.package I/MediaBrowserService: No root for client com.your.package.name from service android.service.media.MediaBrowserService$ServiceBinder$1

which caused the onConnectionFailed to be called on my controller activity.

It was happening because i was using the default override implementation of onGetRoot[MediaBrowserServiceCompat] which ultimately caused this issue :

@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {

    return null;
}

this implementation must return a BrowserRoot object with a unique root id instead of null.

@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {

    Log.d("MEDIA_BROWSER_SERVICE_COMPAT","onGetRoot() called");

    return new BrowserRoot(
            'YOUR_UNIQUE_MEDIA_ROOT_ID',
            null);
}

YOUR_UNIQUE_MEDIA_ROOT_ID is a unique string identifier that needs to be passed along, creating a new BrowserRoot for your controller hook.

like image 27
MantiCore Avatar answered Oct 18 '22 03:10

MantiCore