10

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:

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.

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?

bigpotato
  • 26,262
  • 56
  • 178
  • 334
  • Didn't this help you? http://stackoverflow.com/questions/25932570/how-to-play-video-with-avplayerviewcontroller-avkit-in-swift – Santosh Sep 06 '16 at 18:39
  • i want a reference to the default player that opens up when playing videos in am embedded browser, not play videos manually with a URL – bigpotato Sep 06 '16 at 18:40

2 Answers2

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.

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

Community
  • 1
  • 1
Quang Hà
  • 4,613
  • 3
  • 24
  • 40
0

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

You're also not supposed to subclass AVPlayerViewController.

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

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272