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