谷歌ChromeCast iOS(Swift 3)无法播放视频

时间:2017-04-27 20:00:32

标签: ios swift chromecast

我需要帮助!一旦我将设备连接到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

1 个答案:

答案 0 :(得分:0)

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