0

Im using SplitViewController and Swift-YouTube-Player to play youtube videos in app. It works perfectly on iPhone but it doesnt work on iPad.

On iPhone, when i trigger the player, it opens MPMoviewPLayer full screen and plays the video, but on iPad it plays at background I think, I can head the video's voice, but video does not appear.

I couldn't find a solution. Do you have any suggestion why player plays under the splitviewcontroller?

here is the trigger code

        timer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: "alertVideo", userInfo: nil, repeats: false)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButton",  name: UIWindowDidBecomeHiddenNotification, object:nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "startVideo",  name: UIWindowDidBecomeVisibleNotification , object:nil)
        let a = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
        a.dimBackground = true
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        let circleView : YouTubePlayerView? = (cell!.contentView.viewWithTag(10)) as? YouTubePlayerView

        circleView?.play()

and this is the YouTubePLayer i use: https://github.com/gilesvangruisen/Swift-YouTube-Player

marmaralone
  • 516
  • 6
  • 11

2 Answers2

0

Solved the problem.

I changed the library with YKMediaPlayerKit. I hold the player's instance and present it :

let youtube : YKYouTubeVideo = YKYouTubeVideo()
var videoArray : [String] = []

self.youtube.contentURL = (NSURL(string: "http://www.youtube.com/watch?v=" + videoArray[indexPath.row]))
            youtube.parseWithCompletion({ (error) -> Void in
                if self.youtube.videos["hd720"] != nil {
                    let url = NSURL(string: (self.youtube.videos["hd720"] as! String))
                    self.timer.invalidate()
                    MBProgressHUD.hideHUDForView(self.view, animated: true)
                    let player = MPMoviePlayerViewController()
                    player.moviePlayer.contentURL = url
                    self.presentMoviePlayerViewControllerAnimated(player)
                    player.moviePlayer.play()
                } else if self.youtube.videos["medium"] != nil {
                    let url = NSURL(string: (self.youtube.videos["medium"] as! String))
                    self.timer.invalidate()
                    MBProgressHUD.hideHUDForView(self.view, animated: true)
                    let player = MPMoviePlayerViewController()
                    player.moviePlayer.contentURL = url
                    self.presentMoviePlayerViewControllerAnimated(player)
                    player.moviePlayer.play()
                } else if self.youtube.videos["small"] != nil {
                    let url = NSURL(string: (self.youtube.videos["small"] as! String))
                    self.timer.invalidate()
                    MBProgressHUD.hideHUDForView(self.view, animated: true)
                    let player = MPMoviePlayerViewController()
                    player.moviePlayer.contentURL = url
                    self.presentMoviePlayerViewControllerAnimated(player)
                    player.moviePlayer.play()
                } else {
                    let refreshAlert = UIAlertController(title: "Error", message: "Video Not Found!", preferredStyle: UIAlertControllerStyle.Alert)
                    presentViewController(refreshAlert, animated: true, completion: nil)
                }
marmaralone
  • 516
  • 6
  • 11
0

My videos still play in fullscreen on iPhone but it wasn't playing fullscreen on iPad. I had to make a change in YouTubePlayer.swift. If you change allowsInlineMediaPlayback to false in this section:

fileprivate func buildWebView(_ parameters: [String: AnyObject]) {
    webView = UIWebView()
    webView.allowsInlineMediaPlayback = false
    webView.mediaPlaybackRequiresUserAction = false
    webView.delegate = self
    webView.scrollView.isScrollEnabled = false
}

it will now play in fullscreen on iPad. Or at least it is working at the time of writing this. I hope that helps.

johnnelm9r
  • 201
  • 2
  • 9