Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift How to update url of video in AVPlayer when clicking on a button?

I have a video player in an IOS App and I want to update the video when I click on a button, but I do not see how to manage this. (Note : it's not a list of video within a queue)

Here is the code for adding the AVPlayer:

let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
..

..
self.ViewForVideo.addSubview(playerViewController.view)
self.addChildViewController(playerViewController)

player.play()
like image 414
Ludo Avatar asked Jan 22 '26 19:01

Ludo


1 Answers

I have done like this : each time I want to change the video I create a new AVPlayer and affect it to the playerViewController.player like this

let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
playerViewController.player = player
like image 163
Ludo Avatar answered Jan 25 '26 13:01

Ludo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!