gpt4 book ai didi

swift - 检测 AVPlayer 视频开始停止事件

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:11 24 4
gpt4 key购买 nike

这是一段非常简单的 avplayer 代码,可以播放队列中的一小部分视频。我的问题。我实际上想在队列中的视频之间暂停。可能吗?

我确实注意到 rate 触发了两次;状态和通知一样只触发一次。

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)

现在,每当当前正在播放的队列项目发生变化时,您都会收到通知。

关于swift - 检测 AVPlayer 视频开始停止事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34840338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com