简体   繁体   中英

How can I detect if my audio streaming url can not be play by iOS device - swift3

I am making an iOS Radio app with Swift 3! Now it is working and playing all the url address audio streaming well. But when I am in my workplace the Wifi has restrictions for Play audios streaming, so the app just works with Mobile Data.

When I click on Play button it change to Pause and does not send any sounds, It does not play and the console does not show any connectivity error or something like that. I want to know when the firewall or proxy of some WIFI network is blocking my app to send some UIAlert and change the Pause icon to Play ( if i do not do it the user will be waiting for the music).

I am using Swift 3.0 Xcode 8 , AVPlayer to play the audio. I have tried .addPeriodicTimerObserver, I have validated the url and did a lot of things but I could not get the result that I am looking for. Can some one help me?

var urlStreaming:String = "http://someurl"
playerStreaming = AVPlayer(url: URL(string: url)!)
playerLayer = AVPlayerLayer(player: playerStreaming)
playerLayer.frame = CGRect(x:0, y: 0, width:1, height: 10)
self.view.layer.addsublayer(playerLayer)

playerStreaming?.play()

My playerStreaming?.play() is not sending me nothing when It can not play because it starts trying to play but after some seconds it stops and it does not send anything. When I using WIFI connection of my workplace it happens, but If i change it to mobile data I can listening the audio. If i play from another wifi connection for example my house, park or starbuck coffee wifi I can listening it too without problems.

I know that the Wifi connection in my workplace has restriction (blocked by firewall or proxy actually I am not sure. )for audio streaming.

I have been looking for a lot of options and I tried all of them but i had gotten the result that I am looking for.

Can you help me?

You can listen for error by

NotificationCenter.default.addObserver(self, selector: #selector(playerItemFailedToPlay(_:)), name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(playerItemFailedToPlay(_:)), name: NSNotification.Name.AVPlayerItemPlaybackStalled, object: nil)

func playerItemFailedToPlay(_ notification: Notification) {
    let error = notification.userInfo?.first(where: { $0.value is Error }) as? Error

}

don't for get to remove the observer

deinit {
    NotificationCenter.default.removeObserver(self)
}

I think i found something to check it. What do you thing @zombie? I checked if the url is valid or how much it takes to play.

PS Maybe I will change the Time Out value , now it is taking to much time.

Console shows:

Error Info: -> Error Domain=NSURLErrorDomain Code= -1001 "the request time out" . i think I can validate if my error is null or not. if it is null I can show a UIAlert

 let urltest = URL(string: yourul)
    let task = URlSession.shared.dataTask(with: urlTest!){
    data, response, error in

    if(error != nil){
    print("Error info: -> \(error!)")

    //I think here I can changes the Image if i found a error 
    }
    else{
     print("It is going to be play without problem")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM