3

これは、キュー内のビデオの小さなコレクションを再生するシンプルな avplayer コードです。私の質問。私は実際にキューのビデオ間で一時停止したいと考えています。出来ますか?

rate が 2 回発生することに注意しました。ステータスは、通知と同様に一度だけ発生します。

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

}
4

1 に答える 1

5

次の行を追加します。

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

現在再生中のキュー アイテムに変更があるたびに通知されるようになりました。

于 2016-01-17T16:34:52.373 に答える