在我的应用程序中使用Chromecast时,我遇到了一个非常大的问题。我使用普通GCKUICastButton
连接到Chromecast。视频效果很好。
我正在为我的应用程序使用默认的Cast接收器。当设备进入休眠状态时,有时Chromecast会停止,有时,在睡眠模式之后,设备会在一段时间后断开连接。经过大量论坛和Stack Overflow问题后,我实现了以下代码
extension GCKSessionManager {
static func ignoreAppBackgroundModeChange() {
let oldMethod = class_getInstanceMethod(GCKSessionManager.self, #selector(GCKSessionManager.suspendSession(with:)))
let newMethod = class_getInstanceMethod(GCKSessionManager.self, #selector(GCKSessionManager.suspendSessionIgnoringAppBackgrounded(with:)))
method_exchangeImplementations(oldMethod, newMethod)
}
func suspendSessionIgnoringAppBackgrounded(with reason: GCKConnectionSuspendReason) -> Bool {
guard reason != .appBackgrounded else { return false }
return suspendSession(with:reason)
}
}
然后在我的代码中我编写了以下行
GCKSessionManager.ignoreAppBackgroundModeChange()
现在突然之间,Chromecast没有断开连接,但是几分钟的睡眠后,它也会断开连接,同时也会杀死应用程序。即使设备进入睡眠状态或进入后台,我如何保留Chromecast播放会话。
因为我正在使用GCKUICastButton
所以我没有使用GCKDeviceManager所以我无法使用GCKDeviceManager使用ignoreAppStateNotification,如果我也可以使用它,你能建议。
我还在AppDelegate中添加了GCKCastOption代码。