0

I'm starting Ios development with Swift. I'm trying to develop a radio app to listen it.

I create an AVPlayer like that :

class RadioPlayer: NSObject {
    static let sharedInstance = RadioPlayer()
    private var player:AVPlayer?
    private var isPlaying = false

func play() {
    player = AVPlayer(URL: NSURL(string: "myURL")!)
    self.startObserve(player!)
    player!.play()
    isPlaying = true
}

I would like to know if buffer is ready to play (to display an "Waiting" image when it's buffering and a "Stop" image when the radio plays).

I add an observer on the rate of the player :

func startObserve(myPlayer: AVPlayer) {
        let options = NSKeyValueObservingOptions([.New, .Old])
        myPlayer.addObserver(self, forKeyPath: "rate", options: options, context: nil)
    }
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

        switch keyPath! {
        case "rate":
            print(change)
        default:
            print("default")
        }
    }
}

But I see in the Apple documentation that rate is 1 when we ask to play.

I don't find a way to know if the stream is ready or not.

Thanks

Romain

Romain Caron
  • 257
  • 1
  • 3
  • 15
  • 1
    You are observing wrong `keyPath`. `rate` is the speed of playback, i.e. `rate == 2` would mean "play the audio 2 times faster than normal". Take a look at this answer and let me know if it solves your issue : http://stackoverflow.com/a/33957364/765298 – Losiowaty Jun 19 '16 at 13:09
  • Thank you @Losiowaty , It work well. I didn't find this post before. – Romain Caron Jun 20 '16 at 07:14

0 Answers0