【问题标题】:Connecting/disconnecting to/from an Chromecast Device with Swift使用 Swift 连接/断开与 Chromecast 设备的连接
【发布时间】:2014-12-11 12:45:58
【问题描述】:

我最近开始使用 Apple 新语言 Swift 开发和 Chromecast 应用程序。 但我一直无法与 Chromecast 设备建立连接。到目前为止它做了什么,它可以在网络上看到 Chromecast。之后会出现一个AlertController(一个AlertController 和一个ActionSheet 一样) 我之所以使用AlertController 是因为ActionSheet 被Apple 弃用了。起初我以为是 ActionSheet 让它不起作用。之后我尝试了不同版本的 ActionController/ActionSheet,但到目前为止没有运气。作为在 Swift 中创建它的参考 我使用了目标 C 中的 Google Cast 示例应用程序。 https://github.com/googlecast/CastHelloText-ios

--更新--

在弹出警报控制器后,我单击一个设备,然后它正在连接并成功。当我尝试断开连接时,它给了我一个异常错误“unexpectedly found nil while unwrapping an Optional value”。我在这行代码中遇到了这个错误。

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

【问题讨论】:

  • 苦苦挣扎,能否请您在 github 上发布演示代码?我看不到共享应用程序屏幕的适当示例(我不需要播放视频)。请..

标签: ios swift chromecast


【解决方案1】:

我认为你需要在 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()
        }))
    }

【讨论】:

  • 感谢您的评论,但事实并非如此 :(
  • 你现在改变了问题,所以我的回答与问题现在的阅读方式无关。
  • 你回答后我改了问题。
  • 确实你做到了,我看不出你为什么不问另一个问题的任何理由。
【解决方案2】:

如果您不投射任何媒体,媒体标题将为 nil,因此请以这种方式使用它:
声明:

var mediaInformation : GCKMediaInformation?

用法:

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

【讨论】:

  • 感谢您的回答。但它仍然为零,这意味着我无法与连接的 Chromecast 设备断开连接。
  • 意味着您仍然收到错误并且应用程序正在崩溃?因为现在即使它是 nil 也不会抛出任何错误,因为它是可选的
  • 感谢您的评论。不,它不再崩溃了。我的意思是代码继续,然后调用“UIAlertController”,当我在 UIAlertController 中按下设备断开连接时,什么也没有发生。它没有断开连接。它以某种奇怪的方式保持联系。也许是因为 MediaTitle 仍然为零?或者连接的设备没有正确连接?老实说,我不知道该去哪里找了。我迷路了:(
  • 那不是因为媒体标题后面再解释
猜你喜欢
  • 2016-01-30
  • 2014-03-26
  • 2014-07-17
  • 1970-01-01
  • 2014-07-19
  • 2018-07-27
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
相关资源
最近更新 更多