当前位置 : 主页 > 手机开发 > ios >

ios – 在Swift中出现“func observeValueForKeyPath(keyPath:NSString,object:AnyObject,change:

来源:互联网 收集:自由互联 发布时间:2021-06-11
我为这样的AVPlayer添加了一个观察者 self.audioPlayer.addObserver(self, forKeyPath: "currentItem.status", options: [NSKeyValueObservingOptions.New, NSKeyValueObservingOptions.Initial], context: nil) 并呼吁 func observeValueFor
我为这样的AVPlayer添加了一个观察者

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

并呼吁

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)")
        }
    }
}

问题是从不调用observeValueForKeyPath.

整个代码

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)
}
你如何观察球员项目.可能是您可能不正确地观察它.这是一个简单的插图,展示如何正确观察AVPlayer的状态,

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)
    }
}

并且在开始或更改歌曲时打印出以下内容,

Unknown
Ready to play
网友评论