如何在设备处于睡眠状态时让ChromeCast会话保持活动状态?

时间:2017-05-22 06:49:29

标签: java android audio chromecast

我有一个可以将本地媒体内容流式传输到Chromecast接收器的应用。这主要是有效的,除了当设备处于睡眠状态不在外部电源上时,会话将在大约5分钟后死亡/断开(从屏幕变为空白时开始测量)。

我已经在这里查看了这个问题:

How do I keep a ChromeCast route alive when my app is in the background on battery power?

...并实施了两个建议的答案。

具体来说,在我的应用程序的清单中,我有:

<uses-permission android:name="android.permission.WAKE_LOCK" />

然后在我用于将媒体内容流式传输到Chromecast接收器的嵌入式HTTP服务器中,我正在做:

PowerManager powerManager = (PowerManager)Environment.getApplicationContext().getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cast-server-cpu");
wakeLock.acquire();

WifiManager wifiManager = (WifiManager)Environment.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "cast-server-net");
wifiLock.acquire();

该代码在服务器线程启动时执行(在选择Chromecast路由时发生),并且在服务器停止之前锁定不会被释放。这种情况发生在我的MediaRouter.Callback实施中,其中(包含上述问题的接受答案)如下所示:

this.mediaRouterCallback = new MediaRouter.Callback() {
    private MediaRouter.RouteInfo autoconnectRoute = null;

    @Override
    public void onRouteSelected(MediaRouter mediaRouter, MediaRouter.RouteInfo routeInfo) {
        if (autoconnectRoute == null) {
            if (isPlaying()) {
                stop();
                playButton.setImageResource(R.drawable.icon_play);
            }

            castDevice = CastDevice.getFromBundle(routeInfo.getExtras());
            if (castServer != null) {
                castServer.stop();
            }

            //start the Chromecast file server
            castServer = new CastHttpFileServer();
            new Thread(castServer).start();
        }

        initCastClientListener();
        initRemoteMediaPlayer();
        launchReceiver();
    }

    @Override
    public void onRouteUnselected(MediaRouter mediaRouter, MediaRouter.RouteInfo routeInfo) {
        if (! doesRouterContainRoute(mediaRouter, routeInfo)) {
            //XXX:  do not disconnect in this case (the unselect was not initiated by a user action); see https://stackoverflow.com/questions/18967879/how-do-i-keep-a-chromecast-route-alive-when-my-app-is-in-the-background-on-batte
            autoconnectRoute = routeInfo;
            return;
        }

        //user disconnected, teardown and stop playback
        if (isPlaying()) {
            stop();
            playButton.setImageResource(R.drawable.icon_play);
        }
        if (castServer != null) {
            castServer.stop();
            castServer = null;
        }

        teardown();
        castDevice = null;
    }

    @Override
    public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
        super.onRouteAdded(router, route);
        if (autoconnectRoute != null && route.getId().equals(autoconnectRoute.getId())) {
            this.onRouteSelected(router, route);
        }
    }

    private boolean doesRouterContainRoute(MediaRouter router, MediaRouter.RouteInfo route) {
        if (router == null || route == null) {
            return false;
        }

        for (MediaRouter.RouteInfo info : router.getRoutes()) {
            if (info.getId().equals(route.getId())) {
                return true;
            }
        }

        return false;
    }
};

当设备处于睡眠状态并且未连接外部电源时,还需要做些什么才能使Chromecast会话保持活动状态?

另外,考虑到此问题仅在设备连接到USB时再现,在这种情况下,调试的有效方法是什么?

修改

这是会话终止时(来自嵌入式HTTP服务器)报告的堆栈跟踪:

java.net.SocketException: Software caused connection abort
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:112)
 at java.net.SocketOutputStream.write(SocketOutputStream.java:157)
 at com.myapp.CastHttpFileServer.writeResponse(CastHttpFileServer.java:124)
 at com.myapp.CastHttpFileServer.run(CastHttpFileServer.java:180)

我还注意到我可以在RemoteMediaPlayer.OnStatusUpdatedListener实施中捕获此事件,例如:

if (lastStatus == MediaStatus.PLAYER_STATE_PLAYING && mediaStatus.getPlayerState() == MediaStatus.PLAYER_STATE_BUFFERING) {
    //buffer underrun/receiver went away because the wifi died; how do we fix this?
}

1 个答案:

答案 0 :(得分:2)

CommonsWare在评论中有权使用它。我在运行Android 7.1.2的设备上进行了测试,并且在大约5分钟的非活动状态和之后进行了Doze mode was causing the network to drop测试,而不管我的应用程序持有的唤醒锁

修复是在清单中添加以下权限:

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

然后更新我的onRouteSelected()回调实现,以便在选择ChromeCast路由时请求禁用打盹模式:

if (Build.VERSION.SDK_INT >= 23) {
    String packageName = context.getPackageName();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (! pm.isIgnoringBatteryOptimizations(packageName)) {
        //reguest that Doze mode be disabled
        Intent intent = new Intent();
        intent.setAction(
            Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));

        context.startActivity(intent);
    }

}

只要用户选择“是”&#39;出现提示时,应用程序的唤醒锁定得到尊重,即使设备未充电,ChromeCast会话也会保持活动状态。