gpt4 book ai didi

android - 具有多项 Activity 的 Chromecast

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:56 26 4
gpt4 key购买 nike

我正在开发一个同时支持 Chromecast 和 ActionbarSherlock 的应用程序。
在每个 Activity 中,我都在 ActionBar 中创建一个 MediaRouteButton。

我的问题是,我没有在第一个 Activity 中连接到 Chromecast 设备,而是转到第二个 Activity。在第二个中,我连接到 Chromecast 设备。跑到第二个Activity的onRouteSelected(),再跑到第一个Activity的onDeviceAvailable() -> openSession()。< br/>我不知道为什么它没有在第二个 Activity 上遇到 onDeviceAvailable()
谁能帮帮我?

提前致谢

这是我在这两个 Activity 中的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cast_sample);

mMetaData = new ContentMetadata();


mMediaSelectionDialog = new MediaSelectionDialog(this);
mCastContext = new CastContext( getApplicationContext());
MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext, this );
mMediaRouter = MediaRouter.getInstance( getApplicationContext() );
mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST );
mMediaRouterCallback = new MyMediaRouterCallback();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.cast_sample, menu);
com.actionbarsherlock.view.MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
mMediaRouteButton = (MediaRouteButton) mediaRouteMenuItem.getActionView();
mMediaRouteButton.setRouteSelector( mMediaRouteSelector );
return true;
}


@Override
protected void onStart() {
super.onStart();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}



@Override
protected void onStop() {
mMediaRouter.removeCallback(mMediaRouterCallback);
super.onStop();
}

private class MyMediaRouterCallback extends MediaRouter.Callback {
@Override
public void onRouteSelected(MediaRouter router, RouteInfo route) {
MediaRouteHelper.requestCastDeviceForRoute(route);
}

@Override
public void onRouteUnselected(MediaRouter router, RouteInfo route) {
try {
if (mSession != null) {
logVIfEnabled(TAG, "Ending session and stopping application");
mSession.setStopApplicationWhenEnding(true);
mSession.endSession();
} else {
Log.e(TAG, "onRouteUnselected: mSession is null");
}
} catch (IllegalStateException e) {
Log.e(TAG, "onRouteUnselected:");
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "onRouteUnselected:");
e.printStackTrace();
}
mMessageStream = null;
mSelectedDevice = null;
}
}


@Override
public void onDeviceAvailable(CastDevice device, String myString,
MediaRouteStateChangeListener listener) {
mSelectedDevice = device;
logVIfEnabled(TAG, "Available device found: " + myString);
openSession();
}
private void openSession() {
mSession = new ApplicationSession(mCastContext, mSelectedDevice);
.....
}

最佳答案

我编写了一个单独的 ChromecastAdapter Singleton 类来实现 MediaRouteAdapter。我在创建时调用 registerMinimalMediaRouteProvider 并且从不注销它。 ChromecastAdapter 包含所有 Chromecast 状态。我在每个 Activity 中所要做的就是传入我的 MediaRouteButton 并对其调用 setRouteSelector。

public class ChromeCastAdapter implements MediaRouteAdapter { 

...
private static ChromeCastAdapter instance = null;

public static ChromeCastAdapter getInstance(Context activity) {
if (instance == null) {
instance = new ChromeCastAdapter(activity);
}
return instance;
}

private ChromeCastAdapter(Context activity) {
this.context = activity.getApplicationContext();

castContext = new CastContext(context);
mediaRouter = MediaRouter.getInstance(context);

MediaRouteHelper.registerMinimalMediaRouteProvider(castContext, this);

mediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST);

mediaRouterCallback = new MediaRouterCallback();
mediaRouter.addCallback(mediaRouteSelector, mediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}

public void setMediaRouteButtonSelector(MediaRouteButton mediaRouteButton) {
mediaRouteButton.setRouteSelector(mediaRouteSelector);
}
...
}

关于android - 具有多项 Activity 的 Chromecast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19722961/

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