Fernando S. Kroes
Fernando S. Kroes

Reputation: 992

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. I used as a reference Google Sample apps at https://github.com/googlecast/CastVideos-ios. 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

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

Upvotes: 1

Views: 1052

Answers (1)

Erik Tjernlund
Erik Tjernlund

Reputation: 1983

As Jesper pointed out, the GCKMediaControlChannel.alloc() line is wrong.

Here's a Swift ported didConnectToCastApplication method (from the Google CastVideos-ios example) that I know works:

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

Upvotes: 1

Related Questions