我正在使用Chromecast Companion Library,第一次创建活动时会显示我的chromecast图标。但是当我离开活动然后回到它时,投射图标将不会显示。以下是我如何添加chromecast图标并更新其显示的相关代码:
在我的布局XML中:
<android.support.v7.app.MediaRouteButton
android:id="@+id/media_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:mediaRouteTypes="user"
android:layout_centerVertical="true"
android:visibility="gone"
/>
在我的活动中:
public void onCreate(Bundle savedInstanceState) {
...
mCastManager = RadioPup.getCastManager(this);
mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
mCastManager.addMediaRouterButton(mMediaRouteButton);
setupCastListener();
}
private void setupCastListener() {
Log.i(LOG_TAG, "SETUP CAST LISTENER");
mCastConsumer = new VideoCastConsumerImpl() {
@Override
public void onCastAvailabilityChanged(boolean castPresent) {
Log.i(LOG_TAG, "CAST AVAILABILITY CHANGED");
mMediaRouteButton.setVisibility(castPresent ? View.VISIBLE :
View.INVISIBLE);
}
@Override
public void onApplicationConnected(ApplicationMetadata appMetadata,
String sessionId, boolean wasLaunched) {
Log.i(LOG_TAG, "CAST APPLICATION CONNECTED");
}
@Override
public void onApplicationDisconnected(int errorCode) {
}
@Override
public void onDisconnected() {
}
@Override
public void onRemoteMediaPlayerMetadataUpdated() {
try {
} catch (Exception e) {
// silent
}
}
@Override
public void onFailed(int resourceId, int statusCode) {
}
@Override
public void onConnectionSuspended(int cause) {
}
@Override
public void onConnectivityRecovered() {
}
};
}
protected void onDestroy() {
...
if (null != mCastManager) {
Log.i(LOG_TAG, "onDestroy()");
mCastManager.clearContext(this);
mCastConsumer = null;
}
}
protected void onPause() {
...
mCastManager.decrementUiCounter();
mCastManager.removeVideoCastConsumer(mCastConsumer);
}
protected void onResume() {
...
mCastManager = RadioPup.getCastManager(this);
mCastManager.incrementUiCounter();
mCastManager.addVideoCastConsumer(mCastConsumer);
}
非常感谢任何帮助。
答案 0 :(得分:0)
您可能希望将incrementUiCounter()
和decrementUiCounter()
添加到您的活动中,请查看CastVideos-android项目中的示例。
答案 1 :(得分:0)
通过使用此回调方法修复它:
@Override
public void onCastDeviceDetected(RouteInfo info) {
Log.i(LOG_TAG, "CAST DEVICE DETECTED");
mMediaRouteButton.setVisibility(View.VISIBLE);
}