检测AVPlayer视频开始停止事件 [英] Detecting AVPlayer video start stop events

查看:109
本文介绍了检测AVPlayer视频开始停止事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一段很好的简单avplayer代码,用于在队列中播放一小部分视频.我的问题.我实际上是想在队列中的视频之间暂停.有可能吗?

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

}

推荐答案

添加此行:

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.

这篇关于检测AVPlayer视频开始停止事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆