Swift:如何访问全屏HTML视频的播放器对象?

时间:2021-04-12 18:58:26

I want to be able to hold a reference to the AVPlayer instance that takes over the screen when playing HTML videos full screen from embedded browsers. My first approach was this:

我希望能够在嵌入式浏览器全屏播放HTML视频时保留对接管屏幕的AVPlayer实例的引用。我的第一个方法是:

extension AVPlayerViewController {

    override public func viewDidAppear(animated: Bool) {
      super.viewDidAppear(animated)

      print("herezzzzzzzzzzzzzzzzzzzzzzzzzzz") // prints correctly
      print(self.player) // prints nil
    }
}

However it always returns nil. So I'm trying a different approach. I want to override either the initializer or play method of AVPlayer but I can't seem to do it without getting objective-c selector conflicts.

然而,它总是返回零。所以我正在尝试一种不同的方法。我想覆盖AVPlayer的初始化器或播放方法,但是如果没有得到objective-c选择器冲突,我似乎无法做到这一点。

import AVKit
import MediaPlayer

extension AVPlayer {
  override func play() { // this doesn't work. just an example of what i want
    super.play()

    print("do stuff here")
  }
}

Is there a way to override one of AVPlayer's instance methods so I can store a reference to self? Or is it not even an AVPlayer?

有没有办法覆盖AVPlayer的一个实例方法,所以我可以存储对自我的引用?或者甚至不是AVPlayer?

2 个解决方案

#1


1  

You can't override methods from an extension. Or more precisely, the compiler allows you to do so, but it doesn't work. See this thread: Overriding methods in Swift extensions

您无法覆盖扩展程序中的方法。或者更确切地说,编译器允许您这样做,但它不起作用。请参阅此主题:覆盖Swift扩展中的方法

You're also not supposed to subclass AVPlayerViewController.

你也不应该继承AVPlayerViewController。

Why can't you just read the AVPlayerViewControllers player property in the class that creates it?

为什么你不能只在创建它的类中读取AVPlayerViewControllers播放器属性?

#2


1  

"The most important classes used for playing video content within that UIWebView are called MPAVController and UIMoviePlayerController". So you can't get the instance of AVPlayer.

“用于在UIWebView中播放视频内容的最重要的类称为MPAVController和UIMoviePlayerController”。所以你无法获得AVPlayer的实例。

Please check this question: How to receive NSNotifications from UIWebView embedded YouTube video playback for more detail.

请检查以下问题:如何从UIWebView嵌入式YouTube视频播放中接收NSNotifications以获取更多详细信息。

#1


1  

You can't override methods from an extension. Or more precisely, the compiler allows you to do so, but it doesn't work. See this thread: Overriding methods in Swift extensions

您无法覆盖扩展程序中的方法。或者更确切地说,编译器允许您这样做,但它不起作用。请参阅此主题:覆盖Swift扩展中的方法

You're also not supposed to subclass AVPlayerViewController.

你也不应该继承AVPlayerViewController。

Why can't you just read the AVPlayerViewControllers player property in the class that creates it?

为什么你不能只在创建它的类中读取AVPlayerViewControllers播放器属性?

#2


1  

"The most important classes used for playing video content within that UIWebView are called MPAVController and UIMoviePlayerController". So you can't get the instance of AVPlayer.

“用于在UIWebView中播放视频内容的最重要的类称为MPAVController和UIMoviePlayerController”。所以你无法获得AVPlayer的实例。

Please check this question: How to receive NSNotifications from UIWebView embedded YouTube video playback for more detail.

请检查以下问题:如何从UIWebView嵌入式YouTube视频播放中接收NSNotifications以获取更多详细信息。