使用Swift进行Chromecast objectForKey

时间:2014-12-17 15:41:34

标签: ios swift google-cast

我开始在iOS中使用Google Cast API,我试图让它与Apple新编程语言Swift一起使用。我在https://github.com/googlecast/CastVideos-ios使用了Google示例应用作为参考。问题是当它连接到一个演员设备并且它到达这里..

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())")
}

它启动并且NSLog正在向我发送"应用程序已启动:false" 然后我收到以下错误.. "终止应用程序到期未被捕获的异常' NSInvalidArgumentException',原因:' setObjectForKey:key不能为nil'" 我查看了Google Cast iOS类文档,我发现了这个{ {3}}

然后我在那个班级找到了这个

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

所以我的想法是我必须把东西放在这里......

   @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)
}

1 个答案:

答案 0 :(得分:1)

正如Jesper所指出的那样,GCKMediaControlChannel.alloc()行是错误的。

以下是我所知道的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()
}