MediaBrowserCompat.ConnectionCallback.onConnected() 在 connect() 之后未被调用
MediaBrowserCompat.ConnectionCallback.onConnected() is not being called after connect()
我正在为 android 创建播放器应用程序并希望从任何 UI class 访问 MediaControllerCompat
。为此,我使用 MediaBrowserCompat
class,我正在尝试将其连接到我的 MediaBrowserServiceCompat
并使用 MediaSessionCompat.Token
获得 MediaControllerCompat
,已在我的 [=] 中设置20=].
我尝试了 MediaBrowserCompat.connect() never calls onConnected or any MediaBrowserCompat.ConnectionCallback method 解决方案,但没有用。
TortoiseActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tortoise);
mediaBrowser = new MediaBrowserCompat(
TortoiseActivity.this,
new ComponentName(TortoiseActivity.this, Service.class),
new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
try {
MediaSessionCompat.Token token = mediaBrowser.getSessionToken();
MediaControllerCompat controller = new MediaControllerCompat(TortoiseActivity.this, token);
MediaControllerCompat.setMediaController(TortoiseActivity.this, controller);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onConnectionFailed() {
Log.e("TAG", "onConnectionFailed");
}
@Override
public void onConnectionSuspended() {
Log.e("TAG", "onConnectionSuspended");
}
},
null);
mediaBrowser.connect();
}
Service.java
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mediaSession = new MediaSessionCompat(this, Service.class.getSimpleName());
setSessionToken(mediaSession.getSessionToken());
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
mediaSession.setCallback(callback);
}
SmallPlayerFragment.java
void initControls(Activity context) {
MediaControllerCompat mediaController = MediaControllerCompat.getMediaController(context);
this.transportControls = mediaController.getTransportControls();
}
结果,我得到了 NullPointer
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.MediaControllerCompat$TransportControls android.support.v4.media.session.MediaControllerCompat.getTransportControls()' on a null object reference
at ru.krivocraft.kbmp.SmallPlayerFragment.initControls(SmallPlayerFragment.java:49)
at ru.krivocraft.kbmp.TortoiseActivity.getSmallPlayerFragment(TortoiseActivity.java:122)
at ru.krivocraft.kbmp.TortoiseActivity.showSmallPlayerFragment(TortoiseActivity.java:248)
at ru.krivocraft.kbmp.TortoiseActivity.access0(TortoiseActivity.java:31)
at ru.krivocraft.kbmp.TortoiseActivity.onItemClick(TortoiseActivity.java:109)
试图获得 MediaControllerCompat.TrasportControls
时。 MediaControllerCompat
绝对是空的。我在 Callback 的 onConnected() 中设置了断点,但线程并没有挂起。所以,我可以得出结论,回调没有被调用。
- 这个答案帮助我解决了问题。事实证明,您甚至无法实现自己的 Binder。在 Service.java
中删除覆盖的 onBind(Intent intent)
回调开始被调用。
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder; //remove this custom binder
}
我正在为 android 创建播放器应用程序并希望从任何 UI class 访问 MediaControllerCompat
。为此,我使用 MediaBrowserCompat
class,我正在尝试将其连接到我的 MediaBrowserServiceCompat
并使用 MediaSessionCompat.Token
获得 MediaControllerCompat
,已在我的 [=] 中设置20=].
我尝试了 MediaBrowserCompat.connect() never calls onConnected or any MediaBrowserCompat.ConnectionCallback method 解决方案,但没有用。
TortoiseActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tortoise);
mediaBrowser = new MediaBrowserCompat(
TortoiseActivity.this,
new ComponentName(TortoiseActivity.this, Service.class),
new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
try {
MediaSessionCompat.Token token = mediaBrowser.getSessionToken();
MediaControllerCompat controller = new MediaControllerCompat(TortoiseActivity.this, token);
MediaControllerCompat.setMediaController(TortoiseActivity.this, controller);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onConnectionFailed() {
Log.e("TAG", "onConnectionFailed");
}
@Override
public void onConnectionSuspended() {
Log.e("TAG", "onConnectionSuspended");
}
},
null);
mediaBrowser.connect();
}
Service.java
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mediaSession = new MediaSessionCompat(this, Service.class.getSimpleName());
setSessionToken(mediaSession.getSessionToken());
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
mediaSession.setCallback(callback);
}
SmallPlayerFragment.java
void initControls(Activity context) {
MediaControllerCompat mediaController = MediaControllerCompat.getMediaController(context);
this.transportControls = mediaController.getTransportControls();
}
结果,我得到了 NullPointer
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.MediaControllerCompat$TransportControls android.support.v4.media.session.MediaControllerCompat.getTransportControls()' on a null object reference
at ru.krivocraft.kbmp.SmallPlayerFragment.initControls(SmallPlayerFragment.java:49)
at ru.krivocraft.kbmp.TortoiseActivity.getSmallPlayerFragment(TortoiseActivity.java:122)
at ru.krivocraft.kbmp.TortoiseActivity.showSmallPlayerFragment(TortoiseActivity.java:248)
at ru.krivocraft.kbmp.TortoiseActivity.access0(TortoiseActivity.java:31)
at ru.krivocraft.kbmp.TortoiseActivity.onItemClick(TortoiseActivity.java:109)
试图获得 MediaControllerCompat.TrasportControls
时。 MediaControllerCompat
绝对是空的。我在 Callback 的 onConnected() 中设置了断点,但线程并没有挂起。所以,我可以得出结论,回调没有被调用。
Service.java
中删除覆盖的 onBind(Intent intent)
回调开始被调用。
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder; //remove this custom binder
}