     }
     
     private func handleStatusChangedEvent() {
-        if player.status == .ReadyToPlay {
+        if player?.status == .ReadyToPlay {
             readyToPlay()
-        } else if player.status == .Failed {
-            self.trigger(.Error, userInfo: ["error": player.currentItem!.error!])
+        } else if player?.status == .Failed {
+            self.trigger(.Error, userInfo: ["error": player!.currentItem!.error!])
         }
     }
     

    }
    
    private func addObservers() {
        player.addObserver(self, forKeyPath: "currentItem.status",
            options: .New, context: &kvoStatusDidChangeContext)
        player.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges",
            options: .New, context: &kvoTimeRangesContext)
        player.addObserver(self, forKeyPath: "currentItem.playbackLikelyToKeepUp",
            options: .New, context: &kvoBufferingContext)
        player.addObserver(self, forKeyPath: "currentItem.playbackBufferEmpty",
            options: .New, context: &kvoBufferingContext)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AVFoundationPlayback.playbackDidEnd),
            name: AVPlayerItemDidPlayToEndTimeNotification, object: player.currentItem)
    }
    
    func playbackDidEnd() {
        trigger(.Ended)
        updateState(.Idle)
        player.seekToTime(kCMTimeZero)
    }
    
    public override func pause() {
        player.pause()
        updateState(.Paused)
    }
    
    public override func stop() {
        player.pause()
        playbackDidEnd()
        removeObservers()
        playerLayer.removeFromSuperlayer()
        player = nil
    }
    
    public override func isPlaying() -> Bool {
        return player != nil &&  player.rate > 0
    }
    
    public override func seekTo(timeInterval: NSTimeInterval) {
        let time = CMTimeMakeWithSeconds(timeInterval, Int32(NSEC_PER_SEC))
        
        player.currentItem?.seekToTime(time)
        trigger(.TimeUpdated, userInfo: ["position" : CMTimeGetSeconds(time)])
    }
    
    public override func duration() -> Double {
        guard playbackType() == .VOD, let item = player.currentItem else {
            return 0
        }

        return CMTimeGetSeconds(item.asset.duration)
    }
    
    public override func playbackType() -> PlaybackType {
        guard player != nil, let duration = player.currentItem?.asset.duration else {
            return .Unknown
        }
        

    }
    
    public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?,
                                                change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        switch context {
        case &kvoStatusDidChangeContext:
            handleStatusChangedEvent()
        case &kvoTimeRangesContext:
            handleTimeRangesEvent()
        case &kvoBufferingContext:
            handleBufferingEvent(keyPath!)
        default:
            break
        }
    }
    
    private func updateState(newState: PlaybackState) {
