Detecting AVPlayer video start stop events

余生长醉 提交于 2019-12-21 20:13:02

问题


Here is nice simple avplayer piece of code playing a small collection of videos in a queue. My question. I actually want to pause between videos on my queue. Is it possible?

I did note that rate fires twice; status fires just once as does notification.

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var VideoView: UIView!
var player:AVQueuePlayer = AVQueuePlayer()

@IBAction func NextSlide(sender: AnyObject) {
    player.play()
}

override func viewDidLoad() {
    func NextSlide(sender: AnyObject) {
    }
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let arrayOfPaths: [String] = ["http://192.100.1.1:8080/dino1.mov","http://192.100.1.1:8080/dino1.mov","http://192.100.1.1:8080/dino1.mov"]

    var shots = [AVPlayerItem]()

    for item in arrayOfPaths {
        let url2adopt = NSURL(string: item)
        let avAsset = AVURLAsset(URL: url2adopt!)
        let shot = AVPlayerItem(asset: avAsset)
        shots.append(shot)
    }

    player = AVQueuePlayer(items: shots)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = VideoView.layer.bounds
    VideoView.layer.addSublayer(playerLayer)
    player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: nil)
    player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
   NSNotificationCenter.defaultCenter().addObserver(self, selector: "itemDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: player.currentItem)

    player.play()
    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// catch changes to status
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if (keyPath == "rate") {
        print(player.rate)
    }
    if (keyPath == "status") {
        print(player.status)
    }
}

func itemDidFinishPlaying(notification:NSNotification) {
    print("finished")
}

}

回答1:


Add this line:

player.addObserver(
    self, forKeyPath:"currentItem", options:.Initial, context:nil)

Now you'll be notified every time there is a change in the queue item currently being played.



来源:https://stackoverflow.com/questions/34840338/detecting-avplayer-video-start-stop-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!