如何动态更改 Google Cast 中的“applicationID”?iOS SDK

Ale*_*rov 5 xcode ios google-cast chromecast swift

application: didFinishLaunching像这样初始化 Google Cast SDK:

let criteria = GCKDiscoveryCriteria(applicationID: kGCKDefaultMediaReceiverApplicationID)
let options = GCKCastOptions(discoveryCriteria: criteria)
GCKCastContext.setSharedInstanceWith(options)
Run Code Online (Sandbox Code Playgroud)

我尝试将视频内容流式传输到默认媒体接收器并且它有效。

但我需要能够更改kGCKDefaultMediaReceiverApplicationID 为自定义接收器应用程序 ID,以将不同的内容发送到我通过 Google Cast SDK 开发者控制台注册的自定义接收器应用程序。我无法重新启动应用程序来指定不同的应用程序 ID。

有没有什么方法可以在应用程序中的某个时刻在GCKCastContext.setSharedInstanceWith(options)调用后动态地执行此操作?

此方法的调用仅有效一次,我无法更新 GCKDiscoveryCriteria:GCKCastContext.setSharedInstanceWith(options)

我使用“google-cast-sdk-no-bluetooth”,版本:4.5.3

在Android SDK中有一个方法可以动态更改接收器应用程序ID:https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastContext#public-void- setreceiverapplicationid-字符串-applicationid

不幸的是,我在 iOS SDK 中没有看到类似的东西:(

我唯一发现的是这个(在 GCKSessionManager 内):

/**
 * Sets the default session options for the given device category.The session options are passed to
 * the GCKDeviceProvider::createSessionForDevice:sessionID:sessionOptions: method when the user
 * selects a device from the Cast dialog.  For Cast sessions, the session options can specify which
 * receiver application to launch.
 *
 * @param sessionOptions The session options. May be <code>nil</code> to remove any previously set
 * options.
 * @param category The device category.
 *
 * @since 4.0
 */
- (void)setDefaultSessionOptions:(nullable GCKSessionOptions *)sessionOptions
               forDeviceCategory:(NSString *)category;
Run Code Online (Sandbox Code Playgroud)

但是没有关于要传递哪个 sessionOptions 和类别的文档(类别可能是 kGCKCastDeviceCategory)。存在哪些参数以及如何构造它们是无法理解的。

Ale*_*rov 2

经过一段时间的逆向工程,我找到了解决方案。它需要两个必要的步骤:

第 1 步:GCKDiscoveryCriteria使用私有 Objective-C 调用进行初始化:

let criteria = GCKDiscoveryCriteria(applicationID: kReceiverAppID)
let initWithIds = Selector(("initWithApplicationIDs:"))
if criteria.responds(to: initWithIds) {
    criteria.perform(initWithIds, with: NSOrderedSet(array: [kGCKDefaultMediaReceiverApplicationID, kReceiverAppID]))
}
    
let options = GCKCastOptions(discoveryCriteria: criteria)
GCKCastContext.setSharedInstanceWith(options)
Run Code Online (Sandbox Code Playgroud)

在这里,您需要提供一个NSOrderedSet带有您想要使用的应用程序 ID 的实例。就我而言,其中之一是默认媒体接收器应用程序 ID,第二个是自定义的。

第 2 步:动态设置新创建的会话要使用的应用程序 ID。您可以在应用程序生命周期中多次调用它:

let sessionManager = GCKCastContext.sharedInstance().sessionManager
sessionManager.setDefaultSessionOptions(["gck_applicationID": NSString("YOUR_RECEIVER_APP_ID")],
                                                                                forDeviceCategory: kGCKCastDeviceCategory)
    
Run Code Online (Sandbox Code Playgroud)

这里我们使用字符串gck_applicationID作为键。我花了好几天的时间试图找到它,终于成功了!您可以将kGCKDefaultMediaReceiverApplicationID其用作值或您的自定义接收器应用程序 ID。

感谢@iUrii 和他的逆向工程指导。/sf/answers/4578564231/

PS:我使用了私有方法来完成我想要的。它适用于google-cast-sdk-no-bluetooth4.5.3 版本。但它可能会发生变化。使用时要小心,新版本的SDK可能会破坏它。