我正在开发小型Android chromecast发送者应用程序。 当我尝试使用MediaRouter API创建新会话时,它无法正常工作。
import android.support.v7.media.MediaControlIntent;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
class PlayerFragment extends Fragment
{
MediaRoute.Callback cb;
/*
....
*/
cb = new MediaRouter.Callback() {
@Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteSelected(router, route);
Log.v("PlayerFragment", "Route selected");
mRoute = route;
createSession();
}
@Override
public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {
super.onRouteUnselected(router, route);
mRoute = null;
}
};
private void startDiscovery(){
mRouter.addCallback(selector, cb, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}
private void createSession(){
Intent intent = new Intent(MediaControlIntent.ACTION_START_SESSION);
intent.addCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK);
intent.putExtra(CastMediaControlIntent.EXTRA_CAST_APPLICATION_ID,
CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID);
intent.putExtra(CastMediaControlIntent.EXTRA_CAST_RELAUNCH_APPLICATION, true);
intent.putExtra(CastMediaControlIntent.EXTRA_DEBUG_LOGGING_ENABLED, true);
intent.putExtra(CastMediaControlIntent.EXTRA_CAST_STOP_APPLICATION_WHEN_SESSION_ENDS,
true);
mRoute.sendControlRequest(intent, new MediaRouter.ControlRequestCallback() {
@Override
public void onResult(Bundle data) {
super.onResult(data);
mSessionId = data.getString(MediaControlIntent.EXTRA_SESSION_ID);
Log.v("PlayerFragment", "Session ID is: " + mSessionId);
}
});
}
}
应用程序没有在电视上启动,logcat中没有相应的字符串,onResult方法中的调试器断点也没有被捕获。 这是logcat的一部分
10-05 23:02:05.097 5740-5740/shirokovoi.ChromeCastSSAPP I/System.out﹕ debugger has settled (1372)
10-05 23:02:05.582 5740-5740/shirokovoi.ChromeCastSSAPP I/MediaRouter﹕ Found default route: MediaRouter.RouteInfo{ uniqueId=android/.support.v7.media.SystemMediaRouteProvider:DEFAULT_ROUTE, name=Tablet, description=null, enabled=true, connecting=false, playbackType=0, playbackStream=3, volumeHandling=1, volume=15, volumeMax=15, presentationDisplayId=-1, extras=null, providerPackageName=android }
10-05 23:02:06.074 5740-5740/shirokovoi.ChromeCastSSAPP D/android.widget.GridLayout﹕ vertical constraints: y1-y0>=500, y2-y1>=32, y3-y2>=48, y4-y3>=0, y4-y0<=512 are inconsistent; permanently removing: y4-y0<=512.
10-05 23:02:06.222 5740-5740/shirokovoi.ChromeCastSSAPP D/OpenGLRenderer﹕ Enabling debug mode 0
10-05 23:02:06.722 5740-5740/shirokovoi.ChromeCastSSAPP I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@419a2a68 time:38947645
10-05 23:02:15.863 5740-5740/shirokovoi.ChromeCastSSAPP V/PlayerFragment﹕ Route selected
答案 0 :(得分:0)
最后,我解决了这个问题。 关键在于我的意图中缺少EXTRA_SESSION_STATUS_UPDATE_RECEIVER。我不知道为什么没有这个EXTRA就行不通,但如果我添加它就行了。