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

若如初见. 提交于 2019-12-23 17:01:03

问题


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()

回答1:


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


来源:https://stackoverflow.com/questions/37763430/swift-how-to-update-url-of-video-in-avplayer-when-clicking-on-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!