Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3. AVPlayerViewController issue when exit from fullscreen

I am getting an error in Console when app exits from fullscreen and screen UI slides to top.

Error:

-[AVPlayerViewController exitFullScreenAnimated:completionHandler:] failed with error View needs to be loaded before you can call -[AVPlayerViewController (0x1028b5000) exitFullScreenAnimated:completionHandler:]!

App plays youtube videos in embedded HTML in UIWebView.

Code:

var webView = UIWebView(frame: self.view.frame)
webView.allowsInlineMediaPlayback = true
webView.mediaPlaybackRequiresUserAction = false
embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='\(self.view.frame.size.width)' height=\(frameHeight) src='http://www.youtube.com/embed/\(youtube_code)?enablejsapi=1&rel=0&playsinline=1&autoplay=1&showinfo=0&modestbranding=0' frameborder='0'></body></html>"

@IBAction func playVideo(_ sender: Any) {
        if let webView = webView {
            self.view.bringSubview(toFront: webView)
            webView.scrollView.isScrollEnabled = false;
            webView.scrollView.bounces = false;
            // Load your webView with the HTML we just set up
            webView.loadHTMLString(embededHTML, baseURL: Bundle.main.bundleURL)
        }
    }

Any help highly appreciated. Thanks.

like image 783
Jasur Rajabov Avatar asked May 29 '18 11:05

Jasur Rajabov


1 Answers

Try replacing UIWebView with WebKit's WKWebView

like image 166
yasirmturk Avatar answered Oct 14 '22 05:10

yasirmturk