1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import UIKit import AVFoundation import AVKit class ViewController: AVPlayerViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) let path1 = Bundle.main.path(forResource: "kh", ofType: "mp4")! let asset1 = AVAsset(url: URL(fileURLWithPath: path1)) let playerItem1 = AVPlayerItem(asset: asset1) self.player = AVPlayer(playerItem: playerItem1) self.player?.actionAtItemEnd = AVPlayer.ActionAtItemEnd.none self.player?.play() } @objc private func playerItemDidReachEnd(_ notification: Notification) { // 動画を最初に巻き戻す self.player?.currentItem?.seek(to: CMTime.zero, completionHandler: nil) } } |