
my code is able to find the cast device , but can't able to make connection and cast.
Hiii,
I am able to find the casting device but unable to cast ....
please find the code below which i am working on
func setUpCastController()
{
castController = JWCastController(player: self.player)
castController?.chromeCastReceiverAppID = kGCKMediaDefaultReceiverApplicationID
castController?.delegate = self
castController?.scanForDevices()
}
func onConnected(to device: JWCastingDevice) {
updateForCastDeviceConnection()
}
func onDisconnected(fromCastingDevice error: Error?) {
updateForCastDeviceDisconnection()
}
func onConnectionTemporarilySuspended() {
updateWhenConnectingToCastDevice()
}
func onConnectionRecovered() {
updateForCastDeviceConnection()
}
func onConnectionFailed(_ error: Error?) {
if error != nil {
print("Connection Error: \(error)")
}
updateForCastDeviceDisconnection()
}
func onCasting() {
updateForCasting()
}
func updateWhenConnectingToCastDevice() {
castingButton?.tintColor = UIColor.white
castingButton?.imageView?.startAnimating()
}
func updateForCastDeviceConnection() {
castingButton?.imageView?.stopAnimating()
castingButton?.setImage(UIImage(named: "cast_on")?.withRenderingMode(.alwaysTemplate), for: .normal)
castingButton?.tintColor = UIColor.blue
}
func updateForCastDeviceDisconnection() {
castingButton?.imageView?.stopAnimating()
castingButton?.setImage(UIImage(named: "cast_off")?.withRenderingMode(.alwaysTemplate), for: .normal)
castingButton?.tintColor = UIColor.white
}
func onCastingDevicesAvailable(_ devices:[JWCastingDevice] ) {
availableDevices = devices as NSArray
if devices.count > 0 && !(castingItem != nil) {
chosenDevice = devices[0]
self.castController?.connect(to: chosenDevice)
setUpCastingButton()
updateForCastDeviceDisconnection()
}
else if devices.count == 0 {
navigationItem.rightBarButtonItems = nil//flag rakhte hoeb
}
}
func setUpCastingButton() {
castingButton = UIButton(frame: CGRect(x: 8, y: 8, width: 22, height: 22))
castingButton?.addTarget(self, action: #selector(self.castButtonTapped), for: .touchUpInside)
prepareCastingButtonAnimation()
videoPlay.addSubview(castingButton!)
self.view.addSubview(castingButton!)
}
func prepareCastingButtonAnimation() {
let connectingImages:[UIImage] = [(UIImage(named: "cast_connecting0")?.withRenderingMode(.alwaysTemplate))!, (UIImage(named: "cast_connecting1")?.withRenderingMode(.alwaysTemplate))!, (UIImage(named: "cast_connecting2")?.withRenderingMode(.alwaysTemplate))!, (UIImage(named: "cast_connecting1")?.withRenderingMode(.alwaysTemplate))!]
castingButton?.imageView?.animationImages = connectingImages as [UIImage]?
castingButton?.imageView?.animationDuration = 2
}
func castButtonTapped() {//1
weak var weakSelf: PlayMovieVC? = self
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.popoverPresentationController?.barButtonItem = castingItem
if castController?.connectedDevice == nil
{//2
alertController.title = "Connect to"
availableDevices.enumerateObjects(using: { (object, index, stop) in
if let device = object as? JWCastingDevice {
if (self.availableDevices.count>0)
{//3
let okAction = UIAlertAction(title: device.name, style: UIAlertActionStyle.default) {//5
UIAlertAction in
weakSelf?.castController?.connect(to: device)
weakSelf?.updateWhenConnectingToCastDevice()
weakSelf?.castController?.cast()
}//5
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
// }//for//4
}//if count//3
}
})//block
}
}//aleart//1