gpt4 book ai didi

android - 无法连接 MediaBrowserCompat

转载 作者:行者123 更新时间:2023-11-29 14:44:43 25 4
gpt4 key购买 nike

我关注了 Android official documentation on connecting MediaBrowserCompat但它拒绝连接,事实上 onConnected()onConnectionSuspended()onConnectionFailed() 都没有被调用。

我也试过this answer但它没有用。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_player);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupMediaComponents();


// initializeViews();
}

private void setupMediaComponents() {
Log.d(TAG, "setupMediaComponents");
setVolumeControlStream(AudioManager.STREAM_MUSIC);

mediaBrowserCompat = new MediaBrowserCompat(this, new ComponentName(this, SongPlayerService.class),
mediaBrowserCompatConnectionCallback, null);
}

@Override
protected void onStart() {
super.onStart();
mediaBrowserCompat.connect();
}

@Override
public void onStop() {
super.onStop();
// (see "stay in sync with the MediaSession")
if (MediaControllerCompat.getMediaController(this) != null) {


MediaControllerCompat.getMediaController(this).unregisterCallback(controllerCallback);
}
mediaBrowserCompat.disconnect();

}

private MediaBrowserCompat.ConnectionCallback mediaBrowserCompatConnectionCallback = new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
Log.d(TAG, "onConnected");
try {
// Create a MediaControllerCompat
MediaControllerCompat mediaController =
new MediaControllerCompat(AudioPlayerActivity.this, mediaBrowserCompat.getSessionToken());

// Save the controller
MediaControllerCompat.setMediaController(AudioPlayerActivity.this, mediaController);

setControlClickListeners();
} catch (RemoteException e) {
e.printStackTrace();
}
}

@Override
public void onConnectionSuspended() {
Log.d(TAG, "onConnectionSuspended");
// We were connected, but no longer :-(
}

@Override
public void onConnectionFailed() {
Log.d(TAG, "onConnectionFailed");
// The attempt to connect failed completely.
// Check the ComponentName!
}
};

我正在使用版本 25.3.1 的支持库,所有 sdk 工具都是最新的。


编辑

SongPlayerService的一部分

@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
// Returning null means no one can connect so we’ll return something
return new BrowserRoot(getString(R.string.app_name), null); // Name visible in Android auto, Bundle is for optional params
// TODO: Support Android auto
}

最佳答案

终于找到问题的根源了。我覆盖了 onBind 并返回了 null

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

删除上面的行为我修复了它。

关于android - 无法连接 MediaBrowserCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com