gpt4 book ai didi

java - 如何在设备 hibernate 时保持 ChromeCast session 有效?

转载 作者:行者123 更新时间:2023-11-29 19:14:26 26 4
gpt4 key购买 nike

我有一个应用可以将本地媒体内容流式传输到 Chromecast 接收器。这主要是有效的,除了当设备处于 sleep 状态并且没有使用外部电源时, session 将在大约 5 分钟后终止/断开连接(从屏幕变黑时开始计算)。

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

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

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

具体来说,在我的应用 list 中,我有:

<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;
}
};

为了在设备处于 sleep 状态且未连接到外部电源时保持 Chromecast session Activity ,还需要做些什么?

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

编辑

这是 session 终止时报告的堆栈跟踪(来自嵌入式 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?
}

最佳答案

CommonsWare 在他的评论中拥有它的权利。我在运行 Android 7.1.2 和 Doze mode was causing the network to drop 的设备上进行测试在闲置大约 5 分钟后,无论我的应用程序持有什么唤醒锁

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

<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);
}

}

只要用户在出现提示时选择"is",应用的唤醒锁定就会生效,即使设备未充电,ChromeCast session 也会保持 Activity 状态。

关于java - 如何在设备 hibernate 时保持 ChromeCast session 有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44106286/

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