簡體   English   中英

使用Swift與Chromecast設備連接/斷開連接

[英]Connecting/disconnecting to/from an Chromecast Device with Swift

我最近開始使用Apple新語言Swift開發和Chromecast應用程序。 但是我一直堅持與Chromecast設備建立連接。 到目前為止,它可以做什么,它可以在網絡上看到Chromecast。 之后,將出現一個AlertController(AlertController與ActionSheet相同)之所以使用AlertController,是因為Apple不贊成使用ActionSheet。 起初,我認為這是ActionSheet使其無法正常工作的原因。 之后,我嘗試分配不同版本的ActionController / ActionSheet,但到目前為止還沒有運氣。作為在Swift中創建此代碼的參考,我使用了Google Cast示例應用,這是Objective C中的功能。https://github.com/ googlecast / CastHelloText-ios

- 更新 -

Alertcontroller彈出窗口后,我單擊一個正在連接的設備並成功。 當我嘗試斷開連接時,它給我一個異常錯誤“ 在展開可選值時意外發現nil ”。 我在這行代碼中收到此錯誤。

self.mediaInformation.metadata.stringForKey(kGCKMetadataKeyTitle)

所以基本上說的是mediaInformation = nil,

self.mediaInformation.metadata.stringForKey(kGCKMetadataKeyTitle!)

所以我認為可以將其設為可選,但這沒用。 有誰知道為什么它不起作用?

func chooseDevice() {
    if selectedDevice == nil {
        let alertController = UIAlertController(title: "Choose an device..", message: "Click on your chromecast!", preferredStyle: .ActionSheet)

        for device in deviceScanner.devices {
            alertController.addAction(UIAlertAction(title: device.friendlyName, style: .Default, handler: { alertAction in
                self.selectedDevice = device as GCKDevice
                self.connectToDevice()
            }))
        }

        let addCancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { alertAction in
            alertController.dismissViewControllerAnimated(true, completion: nil)
        })

        // Add action to the controller
        alertController.addAction(addCancelAction)

        // Finaly present the action controller
        presentViewController(alertController, animated: true, completion: nil)
    }
    else {
        updateButtonStates()

        var mediaTitle = GCKMediaInformation()
        mediaTitle.metadata.stringForKey(self.textFieldUrl.text)

        let alertController = UIAlertController(title: "Casting to: \(selectedDevice.friendlyName)", message: nil, preferredStyle: .ActionSheet)

        let addDisconnectingAction = UIAlertAction(title: "Disconnect device", style: .Destructive, handler: { alertAction in
            println("De waarde van mediaInformation is: \(self.mediaInformation)")
            if self.mediaInformation != nil {
                (self.mediaInformation != nil ? 1 : 0)
                alertController.dismissViewControllerAnimated(true, completion: nil)
                println("the else UIAlertController")
            }
        })

        let addCancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { alertAction in
            println("De waarde van mediaInformation is: \(self.mediaInformation)")
            if self.mediaInformation != nil {
                (self.mediaInformation != nil ? 2 : 1)
                alertController.dismissViewControllerAnimated(true, completion: nil)
                println("else uiactionsheet")
            }
        })

        alertController.addAction(addDisconnectingAction)
        alertController.addAction(addCancelAction)

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

這就是我與Chromecast建立連接的方式。 connectToDevice()deviceManagerDidConnect()可能有問題嗎? 令人討厭的是,我在deviceManagerDidConnect()從未收到消息“已連接”

func connectToDevice() {
    if selectedDevice != nil {
        var info = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String
        deviceManager = GCKDeviceManager(device: selectedDevice, clientPackageName: info)

        NSLog("De waarde van info: \(info)")
        NSLog("De waarde van deviceManager in connectToDevice() is: \(deviceManager)")

        deviceManager = GCKDeviceManager(device: deviceScanner.devices[0] as GCKDevice, clientPackageName: info)
        deviceManager.delegate = self
        deviceManager.connect()
    }
}

    func deviceManagerDidConnect(deviceManager: GCKDeviceManager!) {
        NSLog("Connected!")

        updateButtonStates()

        deviceManager.launchApplication(kReceiverAppID)
}

我認為您需要在處理程序中執行一些操作,以使UIAlertAction設置self.selectedDevice

例如

    for selectedDevice in self.deviceScanner.devices {
        alertController.addAction(UIAlertAction(title: selectedDevice.friendlyName, style: .Default, handler: { action in
            self.selectedDevice = device
            self.connectToDevice()
        }))
    }

如果您不投射任何媒體,則媒體標題將為nil,請按以下方式使用它:
宣言:

var mediaInformation : GCKMediaInformation?

用法:

let mediaTitle = self.mediaInformation?.metadata.stringForKey(kGCKMetadataKeyTitle)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM