I am playing video using avkit. so I have multiple video files and i want to play next and previous song on the button click. i am doing this to play video -:
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)
}
and in view will appear i am doing this -:
playVideo()
player.play()
This is what i am doing on next and previous button -:
@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()
}
}
But this is not working fine what is wrong with this code
CodePudding user response:
As far as I understood you are not removing the previous video layer before adding the new one. Thats why multiple video sounds come together.
Following example works for me.
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()
}
CodePudding user response:
Declare class variable var player = AVPlayer?
and in you playVideo()
function add self.player = player
. When you want to replace video try this:
func replaceVideo() {
let playerItem = AVPlayerItem(url: URL(string: "videoURL")!)
self.player.replaceCurrentItem(with: playerItem)
}