我正在创建一个通用的Chromecast远程控制应用。该应用程序的大部分内容已经创建,我已设法使Chromecast音量控制正常工作(通过连接到另一个正在投放的应用程序的Chromecast设备 - 例如YouTube)。
我遇到的困难是执行其他媒体命令,例如播放,暂停,搜索等。
用例示例: 1.用户在他们的Android设备上打开YouTube并开始投射视频。 2.用户打开我的应用并连接到同一个Chromecast设备。 3.我的应用程序的音量控制(现在工作) 4.媒体控制(播放,暂停等)(尚未奏效)
我找到了Cast api引用,它解释了你可以使用media命令发送消息(ApiClient,命名空间,消息);但是"消息" (JSON)需要当前应用程序的sessionId(在本例中为Youtube)。我尝试了以下内容,但与当前应用程序的连接总是失败; status.isSuccess()始终为false:
Cast.CastApi
.joinApplication(mApiClient)
.setResultCallback(
new ResultCallback<Cast.ApplicationConnectionResult>() {
@Override
public void onResult(
Cast.ApplicationConnectionResult result) {
Status status = result.getStatus();
if (status.isSuccess()) {
ApplicationMetadata applicationMetadata = result
.getApplicationMetadata();
sessionId = result.getSessionId();
String applicationStatus = result
.getApplicationStatus();
boolean wasLaunched = result
.getWasLaunched();
Log.i(TAG,
"Joined Application with sessionId: "
+ sessionId
+ " Application Status: "
+ applicationStatus);
} else {
// teardown();
Log.e(TAG,
"Could not join application: "
+ status.toString());
}
}
});
是否可以从通用远程控制应用程序(如我正在创建的应用程序)获取已在运行的强制转换应用程序的sessionId?如果是这样,我是否正确地假设我可以使用以下内容在连接的Chromecast设备上执行媒体命令:
JSONObject message = new JSONObject();
message.put("mediaSessionId", sessionId);
message.put("requestId", 9999);
message.put("type", "PAUSE");
Cast.CastApi.sendMessage(mApiClient,
"urn:x-cast:com.google.cast.media", message.toString());
更新: 我已经尝试了@Ali Naddaf提供的建议,但不幸的是他们没有工作。在onCreate中创建mRemoteMediaPlayer之后,我还在onConnected回调中(在ConnectionCallbacks中)执行requestStatus(mApiClient)。当我尝试.play(mApiClient)时,我得到一个IllegalStateException,声明没有当前的媒体会话。另外,我尝试了joinApplication并在回调中执行了result.getSessionId;返回null。
答案 0 :(得分:0)
一些评论和答案:
launchApplication
或joinApplication
的回调中获取sessionId;在“onResult(result)
”中,您可以从result.getSessionId()
RemoteMediaPlayer
并使用那里提供的播放/暂停?每当您通过官方频道播放媒体时,请始终使用RemoteMediaPlayer
(创建后不要忘记在其上调用requestStatus()
。)答案 1 :(得分:0)
是的,有可能,首先你必须保存sesionId和CastDevice设备ID 当从后台删除应用程序并再次打开应用程序时,请检查是否有sessionId然后调用bello行。 Cast.CastApi.joinApplication(apiClient,APP_ID,sid).setResultCallback(connectionResultCallback); 如果获得成功结果,则需要在connectionResultCallback侦听器中实现进一步的过程。
//获取您之前选择的选定设备
@Override
public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
// Log.d("Route Added", "onRouteAdded");
/* if (router.getRoutes().size() > 1)
Toast.makeText(homeScreenActivity, "'onRouteAdded :: " + router.getRoutes().size() + " -- " + router.getRoutes().get(1).isSelected(), Toast.LENGTH_SHORT).show();
else
Toast.makeText(homeScreenActivity, "'onRouteAdded :: " + router.getRoutes(), Toast.LENGTH_SHORT).show();*/
if (router != null && router.getRoutes() != null && router.getRoutes().size() > 1) {
// Show the button when a device is discovered.
// Toast.makeText(homeScreenActivity, "'onRouteAdded :: " + router.getRoutes().size() + " -- " + router.getRoutes().get(1).isSelected(), Toast.LENGTH_SHORT).show();
mMediaRouteButton.setVisibility(View.VISIBLE);
titleLayout.setVisibility(View.GONE);
castName.setVisibility(View.VISIBLE);
selectedDevice = CastDevice.getFromBundle(route.getExtras());
routeInfoArrayList = router.getRoutes();
titleLayout.setVisibility(View.GONE);
if (!isCastConnected) {
String deid = MyPref.getInstance(homeScreenActivity).readPrefs(MyPref.CAST_DEVICE_ID);
for (int i = 0; i < routeInfoArrayList.size(); i++) {
if (routeInfoArrayList.get(i).getExtras() != null && CastDevice.getFromBundle(routeInfoArrayList.get(i).getExtras()).getDeviceId().equalsIgnoreCase(deid)) {
selectedDevice = CastDevice.getFromBundle(routeInfoArrayList.get(i).getExtras());
routeInfoArrayList.get(i).select();
ReSelectedDevice(selectedDevice, routeInfoArrayList.get(i).getName());
break;
}
}
}
}
}
//重新连接Google Api客户端
public void reConnectGoogleApiClient(){
if (apiClient == null) {
Cast.CastOptions apiOptions = new
Cast.CastOptions.Builder(selectedDevice,castClientListener).build();
apiClient = new GoogleApiClient.Builder(this)
.addApi(Cast.API, apiOptions)
.addConnectionCallbacks(reconnectionCallback)
.addOnConnectionFailedListener(connectionFailedListener)
.build();
apiClient.connect();
}
}
//加入申请
private final GoogleApiClient.ConnectionCallbacks reconnectionCallback = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
// Toast.makeText(homeScreenActivity, "" + isDeviceSelected(), Toast.LENGTH_SHORT).show();
try {
String sid = MyPref.getInstance(homeScreenActivity).readPrefs(MyPref.CAST_SESSION_ID);
String deid = MyPref.getInstance(homeScreenActivity).readPrefs(MyPref.CAST_DEVICE_ID);
if (sid != null && deid != null && sid.length() > 0 && deid.length() > 0)
Cast.CastApi.joinApplication(apiClient, APP_ID, sid).setResultCallback(connectionResultCallback);
isApiConnected = true;
} catch (Exception e) {
}
}
@Override
public void onConnectionSuspended(int i) {
isCastConnected = false;
isApiConnected = false;
}
};