简体   繁体   English

从UITableViewController更改视频以在AVPlayerViewController中播放

[英]Change the video to play in AVPlayerViewController from UITableViewController

I have an AVPlayerViewController that reproduces some video streams via URL and an UITableViewController that contains on its cells the urls that user can select to reproduce. 我有一个AVPlayerViewController通过URL复制一些视频流,还有一个UITableViewController在其单元格上包含用户可以选择复制的URL。 When I click on any cell I call the function for change the stream to reproduce (setCanaleDaRiprodurre declared in AvPlayerViewController) passing the new stream url. 当我单击任何单元格时,我都会调用用于更改要重播的流的函数(在AvPlayerViewController中声明为setCanaleDaRiprodurre),并传递新的流URL。 The problem is that the function doesn't stop the player and doesn't change the stream to reproduce. 问题在于该功能不会停止播放器,也不会更改要重播的流。 Seems like I'am calling another instance of AvPlayerController and it isn't right, maybe i need a singleton instance ? 似乎我正在调用AvPlayerController的另一个实例,这是不对的,也许我需要一个单例实例? I link the code of 2 classes and storyboard structure. 我链接了2个类的代码和情节提要结构。

class VideoPlayerController: AVPlayerViewController {

//static let sharedInstance = VideoPlayerController()

override func viewDidLoad() {

    guard let url = URL(string: "http://live1.msf.ticdn.it/Content/HLS/Live/Channel(CH01HA)/Stream(03)/index.m3u8") else { return }
    let playerItem = AVPlayerItem(url: url)
    player = AVPlayer(playerItem: playerItem)
    player?.play()
}

func setCanaleDaRiprodurre(url:String){

    let url1 = URL(string:url)
    let playerItem = AVPlayerItem(url: url1!)
    player?.pause()
    player?.replaceCurrentItem(with: playerItem)
    player?.play()
    print("Hello") 
}

Extract of table cell action: 表单元动作摘录:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let  VideoPlayerController = self.storyboard?.instantiateViewController(withIdentifier: "player") as! VideoPlayerController
    print(menuItems[indexPath.row].stream_url)
    print("sta andanndddooooo")
    VideoPlayerController.setCanaleDaRiprodurre(url: menuItems[indexPath.row].stream_url!)
    //VideoPlayerController.sharedInstance.setCanaleDaRiprodurre(url: menuItems[indexPath.row].stream_url!)

     }

Storyboard 故事板

Each time you are calling: let VideoPlayerController = self.storyboard?.instantiateViewController(withIdentifier: "player") as! VideoPlayerController 每次调用时: let VideoPlayerController = self.storyboard?.instantiateViewController(withIdentifier: "player") as! VideoPlayerController let VideoPlayerController = self.storyboard?.instantiateViewController(withIdentifier: "player") as! VideoPlayerController will create a NEW instance of the View Controller. let VideoPlayerController = self.storyboard?.instantiateViewController(withIdentifier: "player") as! VideoPlayerController将创建视图控制器的NEW实例。

AVPlayerViewController acts pretty much in the same way as any view controller, it is generally full screen and presented as needed unless you embed it as a child view controller and add its view manually to another VC. AVPlayerViewController行为几乎与任何视图控制器相同,它通常是全屏显示,并根据需要进行显示,除非您将其作为子视图控制器嵌入并手动将其视图添加到另一个VC。

Either way, you need to maintain a single instance of the player view controller for this to work. 无论哪种方式,您都需要维护播放器视图控制器的单个实例才能起作用。 If you can explain exactly what you are doing and what your trying to achieve I can provide an example of how to do it. 如果您可以确切说明您在做什么以及您要达到的目标,那么我可以提供一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM