简体   繁体   English

带有Swift的Chromecast objectForKey

[英]Chromecast objectForKey with Swift

I started to work with Google Cast API in iOS and I'm trying to get it to work with Apple new programming language Swift. 我开始在iOS中使用Google Cast API,并试图使其与Apple新的编程语言Swift一起使用。 I used as a reference Google Sample apps at https://github.com/googlecast/CastVideos-ios . 我在https://github.com/googlecast/CastVideos-ios上将 Google Sample应用作为参考。 The problem is when it connect to a cast device and it gets here.. 问题是当它连接到投射设备并到达此处时。

func deviceManager(deviceManager: GCKDeviceManager!, didConnectToCastApplication applicationMetadata: GCKApplicationMetadata!, sessionID: String!, launchedApplication: Bool) {
    NSLog("application has launched \(launchedApplication)")

    mediaControlChannel = GCKMediaControlChannel.alloc()
    mediaControlChannel.delegate = self
    self.deviceManager.addChannel(mediaControlChannel)
    mediaControlChannel.requestStatus()
    NSLog("waarde van requestStatus is: \(mediaControlChannel.requestStatus())")
}

It launches and the NSLog is giving me this "application has launched : false" and then I get the following error.. "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'setObjectForKey: key cannot be nil'" I have looked in the Google Cast iOS class documentation and I found this https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_media_metadata 它启动,NSLog给我这个“应用程序已启动:false” ,然后出现以下错误。。 “由于未捕获的异常'NSInvalidArgumentException',正在终止应用程序,原因:'setObjectForKey:键不能为零'”在Google Cast iOS类文档中,我找到了这个https://developers.google.com/cast/docs/reference/ios/interface_g_c_k_media_metadata

Then I found this in that class 然后我在那个班上发现了这个

- (id) objectForKey:     (NSString *)   key 
Reads the value of a field.

So my thought was that I have to put something here.. 所以我的想法是我必须在这里放些东西。

   @IBAction func sendVideo(sender: AnyObject) {
    NSLog("Cast video")

    if deviceManager == nil {
        var alert = UIAlertController(title: "Not connected", message: "Please connect to Cast device", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        presentViewController(alert, animated: true, completion: nil)

        return
    }

    // Define media metadata
    var metaData = GCKMediaMetadata(metadataType: GCKMediaMetadataType.User)
    metaData.setString("Bug Bunny!", forKey: kGCKMetadataKeyTitle)
    metaData.setString("dit is allemaal maar was subtitles dasdsadhjsdfgjkkHDHSAGDH", forKey: kGCKMetadataKeySubtitle)

    var url = NSURL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg")
    metaData.addImage(GCKImage(URL: url, width: 480, height: 360))

    var mediaInformation = GCKMediaInformation(contentID: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg", streamType: .None, contentType: "video/mp4", metadata: metaData as GCKMediaMetadata, streamDuration: 0, customData: nil)


    NSLog("waarde van mediainformation is : \(mediaInformation)")

    mediaControlChannel.loadMedia(mediaInformation, autoplay: true, playPosition: 0)
}

As Jesper pointed out, the GCKMediaControlChannel.alloc() line is wrong. 正如Jesper指出的那样, GCKMediaControlChannel.alloc()行是错误的。

Here's a Swift ported didConnectToCastApplication method (from the Google CastVideos-ios example) that I know works: 这是我知道可以使用的Swift移植的didConnectToCastApplication方法(来自Google CastVideos-ios示例):

func deviceManager(deviceManager: GCKDeviceManager!, didConnectToCastApplication applicationMetadata: GCKApplicationMetadata!, sessionID: String!, launchedApplication: Bool) {

    self.isReconnecting = false
    self.mediaControlChannel = GCKMediaControlChannel()
    self.mediaControlChannel?.delegate = self
    self.receiverCommandChannel = ChromecastCommandChannel()
    self.receiverCommandChannel?.delegate = self

    self.deviceManager?.addChannel(self.mediaControlChannel)
    self.deviceManager?.addChannel(self.receiverCommandChannel)
    self.mediaControlChannel?.requestStatus()
    self.receiverCommandChannel?.requestStatus()

    self.applicationMetadata = applicationMetadata
    self.updateCastIconButtonStates()

    if let delegate = self.delegate {
        if let device = self.selectedDevice {
            delegate.didConnectToDevice(device)
        } else {
            NSLog("Can not connect to device as no selected device is set")
        }
    } else {
        NSLog("Can not run didConnectToDevice as delegate is not set")
    }

    // Store sessionId in case of restart
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(sessionID, forKey: "lastCCSessionId")
    if let deviceId = self.selectedDevice?.deviceID {
        defaults.setObject(deviceId, forKey: "lastCCDeviceId")
    }
    defaults.synchronize()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM