             debug("Playing current Item")
             self.player!.play()
             self.currentState = .Playing
-            self.delegate?.twistStatusChanged()
+            self.triggerPlaybackStateChanged()
         }
     }
 


        // TODO:
        // Seek to 0 if time is more than 5 seconds, else go to previous
        // Check the repeat
        // Check the shuffle settings
        if self.currentIndex != 0 {
            self.stop()
            self.play(self.currentIndex - 1)
        }
    }

                debug("Received a message for player: \(player)")
            }
            
            if self.currentPlayerItem != nil && object is AVPlayerItem {
                guard let keyPath = keyPath else { return }
                switch keyPath {
                case kStatusKey:
                    if self.currentPlayerItem!.status == .ReadyToPlay {
                        self.player!.play()
                        self.currentState = .Playing
                        self.triggerPlaybackStateChanged()
                    } else {
                        debug("Status updated but not ready to play")
                    }
                case kLoadedTimeRangesKey:
                    if let availableDuration = self.availableDurationForCurrentItem() {
                        let duration = self.currentPlayerItem!.duration
                        let totalDuration = CMTimeGetSeconds(duration)
                        self.delegate?.twist(self, loaded: availableDuration, outOf: totalDuration)
                    }
                default:
                    print("Unhandled key :\(keyPath)")
                }
            }
        } else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }
    }
    
    func availableDurationForCurrentItem() -> NSTimeInterval? {
        guard let currentItem = self.currentPlayerItem else { return nil }
        let loadedTimeRanges = currentItem.loadedTimeRanges
        if let timeRange = loadedTimeRanges.first?.CMTimeRangeValue {
            let startSeconds = CMTimeGetSeconds(timeRange.start)
            let durationSeconds = CMTimeGetSeconds(timeRange.duration)
            return startSeconds + durationSeconds
        }
        return nil
    }
    
    func triggerPlaybackStateChanged() {
        self.delegate?.twistStatusChanged()
        self.updateMPRemoteCommandButtons()
    }
    
    func updateMPRemoteCommandButtons() {
        let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()
        commandCenter.nextTrackCommand.enabled = self.hasNextItem
        commandCenter.previousTrackCommand.enabled = self.hasPreviousItem
        commandCenter.playCommand.enabled = self.currentState == .Paused
        commandCenter.pauseCommand.enabled = self.currentState == .Playing
        commandCenter.togglePlayPauseCommand.enabled = self.isPlayable()
    }
}