【问题标题】:Google ChromeCast iOS (Swift 3) can't play videoGoogle ChromeCast iOS (Swift 3) 无法播放视频
【发布时间】:2017-09-25 18:55:51
【问题描述】:

我需要帮助!一旦我将设备连接到 chromecast,它在即将加载时就崩溃了。我不知道为什么会这样。我关注了 GoogleCast for Docs 和一些示例,似乎它遗漏了一些东西。你们能帮帮我吗?

这是我的代码

这是在我的 OnCreate 上

override func viewDidLoad() {
        sessionManager = GCKCastContext.sharedInstance().sessionManager
        sessionManager?.add(self)
        castMediaController = GCKUIMediaController()

        if isCastEnabled() {
            playSelectedItemRemotely()
        }
}

    //////////////////////////////////////////////////////
    //                                                  //
    //            Start du Google ChromeCast            //
    //                                                  //
    //////////////////////////////////////////////////////



    private func buildMediaInformation() -> GCKMediaInformation {

        let metadata = GCKMediaMetadata(metadataType: GCKMediaMetadataType.generic)
        metadata.setString("Title", forKey: kGCKMetadataKeyTitle)
        metadata.setString("Studio", forKey: kGCKMetadataKeyStudio)

        let mediaInfo = GCKMediaInformation(contentID: "streamURL",
                                                 streamType: GCKMediaStreamType.none,
                                                 contentType: "video/m3u",
                                                 metadata: metadata,
                                                 streamDuration: 60,
                                                 mediaTracks: nil,
                                                 textTrackStyle: nil,
                                                 customData: nil)
        print(mediaInfo.contentID)
        return mediaInfo
    }


    func isCastEnabled() -> Bool {
        switch GCKCastContext.sharedInstance().castState {
        case GCKCastState.connected:
            print("cast connected")
            return true
        case GCKCastState.connecting:
            print("cast connecting")
            return true
        case GCKCastState.notConnected:
            print("cast notConnected")
            return false
        case GCKCastState.noDevicesAvailable:
            print("cast noDevicesAvailable")
            return false
        }
    }

    func playSelectedItemRemotely() {

        let castSession = GCKCastContext.sharedInstance().sessionManager.currentCastSession
        if (castSession != nil) {
            castSession?.remoteMediaClient?.loadMedia(self.buildMediaInformation(), autoplay: true)
            self.dismiss(animated: true, completion: nil)
        }
        else {
            print("no castSession!")
        }
    }




    func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKSession) {
        playSelectedItemRemotely()
    }

    func sessionManager(_ sessionManager: GCKSessionManager, didResumeSession session: GCKSession) {

    }

    func sessionManager(_ sessionManager: GCKSessionManager, didEnd session: GCKSession, withError error: Error?) {
        let castSession = GCKCastContext.sharedInstance().sessionManager.currentCastSession
        castSession?.endAndStopCasting(true)
    }

    func sessionManager(_ sessionManager: GCKSessionManager, didFailToStart session: GCKSession, withError error: Error) {

    }


    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

//        playerView.player?.replaceCurrentItem(with: nil)
//        
//        //this stops the session manager sending callbacks to your VideoVC
//        sessionManager?.remove(self)



    }

这是在我的 AppDelegate 上

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let castAppID = "myKey"
        let options = GCKCastOptions.init(receiverApplicationID: castAppID)
        GCKCastContext.setSharedInstanceWith(options)
        GCKLogger.sharedInstance().delegate = self

【问题讨论】:

标签: ios swift chromecast


【解决方案1】:

我明白了。我的问题出在 Google Cast 控制台中,我有一个远程接收器。需要有一个自定义样式的接收器或媒体。所以确保你有这个权利!!或者只是在您的 AppID 上使用 kGCKMediaDefaultReceiverApplicationID 来获取默认值。

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 2018-01-03
    • 2014-11-13
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2017-02-10
    • 2014-08-17
    • 2015-05-29
    相关资源
    最近更新 更多