1

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?

Cristofer
  • 1,046
  • 13
  • 21

2 Answers2

0

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)
}
zombie
  • 5,069
  • 3
  • 25
  • 54
  • Thanks! I am going to check it right now! I think I already used it but I am going to test it again! :) Should I write it in viewDidLoad func? or after playerStreaming?.play() – Cristofer Aug 03 '17 at 20:08
  • @Cristofer just add the observer any time you see suitable before you start playing so you will get the event – zombie Aug 03 '17 at 20:13
  • I did not work bro! thank you anyway! I posted below my current solution, I am going to try to find up another better one! @zombie – Cristofer Aug 03 '17 at 20:44
  • I already changed and use to answer it is better for the perfomance. Just I added AVPlayerItemPlaybackStalledNotification – Cristofer Aug 06 '17 at 21:58
  • @Cristofer thank you for the feedback i updated my answer but how do you get the error for AVPlayerItemPlaybackStalled i'm not sure if there is any better way that what i did – zombie Aug 07 '17 at 09:06
  • @anitteb at least explain what you’re doing. – zombie Feb 17 '18 at 23:42
  • I'm playing a radio and added this code snippet above what you've recommended. – anitteb Feb 19 '18 at 08:14
  • Should the stream url send me an error to get this work? – anitteb Feb 19 '18 at 09:13
  • @anitteb I think your question is answered here https://developer.apple.com/documentation/avfoundation/avplayeritemplaybackstallednotification – zombie Feb 19 '18 at 09:18
  • @zombie can you explain `This notification may be posted on a different thread than the one on which the observer was registered.` what does this mean in practice? – anitteb Feb 19 '18 at 10:39
  • Might receive it on a background thread and should not update the layout before checking the thread – zombie Feb 19 '18 at 10:42
  • @zombie ah, ok. anyway, the selector is not getting called :( – anitteb Feb 19 '18 at 11:24
  • @zombie I'm testing bad stream by adding plus characters to it's url. What's your opinion, is the same like original url not sending any data? – anitteb Feb 19 '18 at 11:35
  • can you please ask a question and show what you're doing – zombie Feb 19 '18 at 11:37
  • @zombie Here it is: https://stackoverflow.com/questions/48866004/ios-detecting-if-an-url-stream-is-working-or-not-with-avplayer – anitteb Feb 19 '18 at 12:13
0

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.

P.S. 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")
    }
}
Cristofer
  • 1,046
  • 13
  • 21
  • I think you’re opening two connection one with URLSession to check the data and one with AVPlayer so do you really need to do that – zombie Aug 03 '17 at 20:47
  • @zombie yes! Now I opened it , but I will add validation, if the error == nil it will try to play if not It will stop all and change the image to Play again! – Cristofer Aug 03 '17 at 20:52
  • If that works for you then keep it but as I remember there was another error notification from avplayer maybe you can check it out because I’m thinking that if the internet is slow then you’re making the app response slower if the link was valid – zombie Aug 03 '17 at 21:42
  • If you can edit your question and add the not valid url then I will try to check the error tomorrow cause it’s late where I am – zombie Aug 03 '17 at 21:45