如何管理自定义视频播放器中的下一个和上一个按钮
How to manage next and previous button in custom video player
我正在使用 avkit 播放视频。所以我有多个视频文件,我想在单击按钮时播放下一首和上一首歌曲。
我这样做是为了播放视频 -:
func playVideo(){
let url = URL(string: historyArray[videoUrlIndex].value(forKey:kConstant.keyName.urlString) as! String)
player = AVPlayer(url: url!)
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resize
player.currentItem?.addObserver(self, forKeyPath: "duration", options: [.new,.initial], context: nil)
addTimeObserver()
videoView.layer.sublayers?.removeAll()
playerLayer.removeFromSuperlayer()
videoView.layer.addSublayer(playerLayer)
pip = AVPictureInPictureController(playerLayer: playerLayer)
}
并且在视图中会出现我正在这样做 -:
playVideo()
player.play()
这就是我在下一个和上一个按钮上所做的 -:
@IBAction func rewindButtonPressed(_ sender: AnyObject) {
if videoUrlIndex > 0 {
videoUrlIndex = videoUrlIndex - 1
playVideo()
player.play()
}
}
@IBAction func fastforwardButtonPressed(_ sender: Any) {
if videoUrlIndex >= 0 {
videoUrlIndex = videoUrlIndex + 1
playVideo()
player.play()
}
}
但这不能正常工作这段代码有什么问题
据我了解,您在添加新视频层之前并没有删除之前的视频层。这就是多个视频声音聚集在一起的原因。
以下示例适合我。
var playerLayer: AVPlayerLayer?
var player: AVPlayer?
var urls : [String] = ["http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4","http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"]
override func viewDidLoad(){
super.viewDidLoad()
self.view.addSubview(self.nextButton)
self. nextButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
self. nextButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
self. nextButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
self. nextButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
self.playVideo(index: 1)
player!.play()
}
func playVideo(index : Int){
let videoURL : URL = URL(string: urls[index])!
player = AVPlayer(url: videoURL)
let newPlayerLayer = AVPlayerLayer(player: player)
newPlayerLayer.frame = self.view.bounds
playerLayer?.removeFromSuperlayer()
self.view!.layer.addSublayer(newPlayerLayer)
playerLayer = newPlayerLayer
}
public lazy var nextButton: UIButton = {
let button = UIButton()
button.backgroundColor = UIColor.black
button.setTitle("Next", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(next(_:)), for: .touchUpInside)
return button
}()
@objc func next(_ sender: UIButton){
self.playVideo(index: 0)
player!.play()
}
声明 class 变量 var player = AVPlayer?
并在 playVideo()
函数中添加 self.player = player
。当你想替换视频时试试这个:
func replaceVideo() {
let playerItem = AVPlayerItem(url: URL(string: "videoURL")!)
self.player.replaceCurrentItem(with: playerItem)
}
我正在使用 avkit 播放视频。所以我有多个视频文件,我想在单击按钮时播放下一首和上一首歌曲。 我这样做是为了播放视频 -:
func playVideo(){
let url = URL(string: historyArray[videoUrlIndex].value(forKey:kConstant.keyName.urlString) as! String)
player = AVPlayer(url: url!)
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resize
player.currentItem?.addObserver(self, forKeyPath: "duration", options: [.new,.initial], context: nil)
addTimeObserver()
videoView.layer.sublayers?.removeAll()
playerLayer.removeFromSuperlayer()
videoView.layer.addSublayer(playerLayer)
pip = AVPictureInPictureController(playerLayer: playerLayer)
}
并且在视图中会出现我正在这样做 -:
playVideo()
player.play()
这就是我在下一个和上一个按钮上所做的 -:
@IBAction func rewindButtonPressed(_ sender: AnyObject) {
if videoUrlIndex > 0 {
videoUrlIndex = videoUrlIndex - 1
playVideo()
player.play()
}
}
@IBAction func fastforwardButtonPressed(_ sender: Any) {
if videoUrlIndex >= 0 {
videoUrlIndex = videoUrlIndex + 1
playVideo()
player.play()
}
}
但这不能正常工作这段代码有什么问题
据我了解,您在添加新视频层之前并没有删除之前的视频层。这就是多个视频声音聚集在一起的原因。
以下示例适合我。
var playerLayer: AVPlayerLayer?
var player: AVPlayer?
var urls : [String] = ["http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4","http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"]
override func viewDidLoad(){
super.viewDidLoad()
self.view.addSubview(self.nextButton)
self. nextButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
self. nextButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
self. nextButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
self. nextButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
self.playVideo(index: 1)
player!.play()
}
func playVideo(index : Int){
let videoURL : URL = URL(string: urls[index])!
player = AVPlayer(url: videoURL)
let newPlayerLayer = AVPlayerLayer(player: player)
newPlayerLayer.frame = self.view.bounds
playerLayer?.removeFromSuperlayer()
self.view!.layer.addSublayer(newPlayerLayer)
playerLayer = newPlayerLayer
}
public lazy var nextButton: UIButton = {
let button = UIButton()
button.backgroundColor = UIColor.black
button.setTitle("Next", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(next(_:)), for: .touchUpInside)
return button
}()
@objc func next(_ sender: UIButton){
self.playVideo(index: 0)
player!.play()
}
声明 class 变量 var player = AVPlayer?
并在 playVideo()
函数中添加 self.player = player
。当你想替换视频时试试这个:
func replaceVideo() {
let playerItem = AVPlayerItem(url: URL(string: "videoURL")!)
self.player.replaceCurrentItem(with: playerItem)
}