Problem with "func observValueForKeyPath (keyPath: NSString, object: AnyObject, change: NSDictionary, context: Void)" in Swift

I have added an observer for AVPlayer like

self.audioPlayer.addObserver(self, forKeyPath: "currentItem.status", options: [NSKeyValueObservingOptions.New, NSKeyValueObservingOptions.Initial], context: nil)

      

And called

func observeValueForKeyPath(keyPath : NSString, object : AnyObject, change : NSDictionary, context : Void) {
    if object as! AVPlayer == self.audioPlayer && keyPath.isEqualToString("currentItem.status")
    {
        let playerStatus = self.audioPlayer.currentItem!.status.rawValue as Int
        if playerStatus == AVPlayerStatus.Failed.rawValue
        {
            self.replaceCurrentItem(self.fileName)
        }
        else if playerStatus == AVPlayerStatus.ReadyToPlay.rawValue
        {
            self.playAudio()
            self.updateDurationPeriodcally()
            self.updateInfo()
        }
        else if playerStatus == AVPlayerStatus.Unknown.rawValue
        {
            print("\(self.audioPlayer.currentItem!.error)")
        }
    }
}

      

The problem is it observeValueForKeyPath

never gets called.

Whole code

func loadPlayer (urlString : NSString)
{
    let url : NSURL = NSURL(string: urlString as String)!

    let length = (url.absoluteString).characters.count as Int

    if length == 0
    {
        return
    }

    let asset = AVURLAsset(URL: url, options: nil)
    let anItem = AVPlayerItem(asset: asset)
    self.audioPlayer = AVPlayer(playerItem: anItem)
    self.audioPlayer.volume = 1.0
    self.audioPlayer.play()

    addObserver(self,
        forKeyPath: ObservatingKeyPath,
        options: NSKeyValueObservingOptions.New,
        context: PlayerStatusObservingContext)

    self.audioPlayer.addObserver(self, forKeyPath: "currentItem.status", options: [NSKeyValueObservingOptions.New, NSKeyValueObservingOptions.Initial], context: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "itemDidFinishPlaying:", name:AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}

      

+3


source to share


1 answer


How do you watch the player. You may have noticed this wrong. Here are simple illustrations showing how to properly monitor AVPlayer status,

class TestViewController: UIViewController
{
    private var player: AVPlayer!
    private var playingIndex = 0

    private var songs = ["UpTown.mp3", "TurnMeOn.mp3"]

    private let ObservatingKeyPath = "currentItem.status"

    private let PlayerStatusObservingContext = UnsafeMutablePointer<Int>(bitPattern: 1)


    override func viewDidLoad()
    {
        super.viewDidLoad()

        createPlayer()
        setupGestures()

        playNext()
    }

    func createPlayer()
    {
        player = AVPlayer()
        player.addObserver(self,
            forKeyPath: ObservatingKeyPath,
            options: NSKeyValueObservingOptions.New,
            context: PlayerStatusObservingContext)
    }

    func setupGestures()
    {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "playNext")
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func playNext()
    {
        let url = NSBundle.mainBundle().URLForResource(songs[playingIndex], withExtension: nil)
        let playerItem = AVPlayerItem(URL: url!)
        player.replaceCurrentItemWithPlayerItem(playerItem)
        player.play()

        playingIndex = (++playingIndex % 2)
    }

    override func observeValueForKeyPath(keyPath: String?,
        ofObject object: AnyObject?,
        change: [NSObject : AnyObject]?,
        context: UnsafeMutablePointer<Void>)
    {
        if context == PlayerStatusObservingContext
        {

            if let change = change as? [String: Int]
            {
                let newChange = change[NSKeyValueChangeNewKey]!
                switch newChange
                {
                    case AVPlayerItemStatus.ReadyToPlay.rawValue:
                        print("Ready to play")
                    case AVPlayerItemStatus.Unknown.rawValue:
                        print("Unknown")
                    default:
                        print("Other status")
                }

            }

            return
        }
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
    }
}

      



When you start or change a song, it outputs the following:

Unknown
Ready to play

      

+7


source







All Articles