Thursday, September 22, 2016

Swift: How to access player object of full screen HTML video?

Leave a Comment

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?

2 Answers

Answers 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

You're also not supposed to subclass AVPlayerViewController.

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

Answers 2

"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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment