Search code examples
iosswiftavfoundationavplayeritem

Thread 5: Simultaneous accesses to 0x10b883638, but modification requires exclusive access


I had asked this question regarding some Apple code and making it work.

I have looked here but the answer there does not solve my problem Although I solved that problem I am not getting the bellow error on the line shown.

Thread 5: Simultaneous accesses to 0x10b883638, but modification requires exclusive access

    private var playerItemContext = 0

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    // Only handle observations for the playerItemContext
    print("jdslfhjkfdhaldfahjkflhajfldashkjfdshkjlas")
    guard context == &playerItemContext else {
        super.observeValue(forKeyPath: keyPath, of: object,change: change, context: context)
        return
    }
    ...

Why is this and how can I fix this?


Solution

  • Try this code:

        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    
        // Only handle observations for the playerItemContext
        guard context == &P2SheetViewController.playerStatusContext else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
            return
        }
    
        if keyPath == #keyPath(AVPlayer.status) {
            let status: AVPlayer.Status
            if let statusNumber = change?[.newKey] as? NSNumber {
                status = AVPlayer.Status(rawValue: statusNumber.intValue)!
            } else {
                status = .unknown
            }
    
            //Switch over status value
            switch status {
            case .readyToPlay:
    
                break
            // Player item is ready to play
            case .failed:
                print(".UKNOWN")
    
                break
            // Player item failed. See error.
            case .unknown:
                print(".UKNOWN")
    
                break
            // Player item is not yet ready.
            @unknown default: //new jul 17
                print(".UKNOWN")
    
                break
            }
    
        }
    }
    

    Hope it helps