从某个点开始视频 Swift
Start video from certain point Swift
我有一个使用 avPlayerLayer 在图像视图中播放的视频。
它 pauses/plays 当调用 videoViewTapped 时(i.t。用户点击 imageView)。还有一个进入全屏模式的选项。
我尝试使用 timeStopped = player.currentTime() 来记录用户选择全屏选项的时间,然后在全屏时使用此点开始播放视频。
目前,当我进入全屏模式时,视频会从头开始重新播放并在几秒钟后冻结,而不是在调用全屏模式时视频所在的位置。下面是我的代码:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var videoView: UIImageView!
@IBOutlet var imageView: UIImageView!
let videoPlayer = AVPlayerViewController()
var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
var timeStopped = CMTime()
override func viewDidLoad() {
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
debugPrint("Logo-Animation4.mp4 not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resize
videoView.layer.addSublayer(avPlayerLayer)
}
//MARK: VIDEO STUFF
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
avPlayerLayer.frame = videoView.layer.bounds
}
@IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
if videoView.tag == 0 {
player.play()
videoView.tag = 1
} else {
player.pause()
timeStopped = player.currentTime()
videoView.tag = 0
}
}
@IBAction func fullScreen(_ sender: Any) {
videoPlayer.player = player
present(videoPlayer, animated: true) {
self.videoPlayer.showsPlaybackControls = true
}
}
}
有人能指出我的错误吗?
您还可以使用 videoPlayer 中的 AVPlayer,这样当用户进入全屏模式时就不必重新分配播放器。
let videoPlayer = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
videoPlayer.player = AVPlayer(url: URL(fileURLWithPath: path))
}
@IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
if videoView.tag == 0 {
videoPlayer.player.play()
videoView.tag = 1
} else {
videoPlayer.player.pause()
videoView.tag = 0
}
}
@IBAction func fullScreen(_ sender: Any) {
present(videoPlayer, animated: true) {
self.videoPlayer.showsPlaybackControls = true
videoPlayer.player.play()
}
}
我有一个使用 avPlayerLayer 在图像视图中播放的视频。
它 pauses/plays 当调用 videoViewTapped 时(i.t。用户点击 imageView)。还有一个进入全屏模式的选项。
我尝试使用 timeStopped = player.currentTime() 来记录用户选择全屏选项的时间,然后在全屏时使用此点开始播放视频。
目前,当我进入全屏模式时,视频会从头开始重新播放并在几秒钟后冻结,而不是在调用全屏模式时视频所在的位置。下面是我的代码:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var videoView: UIImageView!
@IBOutlet var imageView: UIImageView!
let videoPlayer = AVPlayerViewController()
var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
var timeStopped = CMTime()
override func viewDidLoad() {
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
debugPrint("Logo-Animation4.mp4 not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resize
videoView.layer.addSublayer(avPlayerLayer)
}
//MARK: VIDEO STUFF
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
avPlayerLayer.frame = videoView.layer.bounds
}
@IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
if videoView.tag == 0 {
player.play()
videoView.tag = 1
} else {
player.pause()
timeStopped = player.currentTime()
videoView.tag = 0
}
}
@IBAction func fullScreen(_ sender: Any) {
videoPlayer.player = player
present(videoPlayer, animated: true) {
self.videoPlayer.showsPlaybackControls = true
}
}
}
有人能指出我的错误吗?
您还可以使用 videoPlayer 中的 AVPlayer,这样当用户进入全屏模式时就不必重新分配播放器。
let videoPlayer = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
videoPlayer.player = AVPlayer(url: URL(fileURLWithPath: path))
}
@IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
if videoView.tag == 0 {
videoPlayer.player.play()
videoView.tag = 1
} else {
videoPlayer.player.pause()
videoView.tag = 0
}
}
@IBAction func fullScreen(_ sender: Any) {
present(videoPlayer, animated: true) {
self.videoPlayer.showsPlaybackControls = true
videoPlayer.player.play()
}
}