1

Hi everyone I am trying to swipe between two pages through a scrollview and it works fine but when I try to swipe while a video is playing it won't work here is my code for my ViewControllers

MainVeiwController

override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.isPagingEnabled=true
        let settings = SettingsViewController(nibName: "SettingsViewController", bundle: nil)
        self.addChildViewController(settings)
        self.scrollView.addSubview(settings.view)
        settings.didMove(toParentViewController: self)

        let diamond = DiamondViewController(nibName: "DiamondViewController", bundle: nil)
        var frame1 = diamond.view.frame
        frame1.origin.x = self.view.frame.size.width
        diamond.view.frame=frame1
        self.addChildViewController(diamond)
        self.scrollView.addSubview(diamond.view)
        diamond.didMove(toParentViewController: self)
        self.scrollView.contentSize = CGSize(width: self.view.frame.size.width * 2, height: self.view.frame.size.height-70)
        scrollView.contentOffset = CGPoint(x: scrollView.frame.size.width, y: 0)
}

Second ViewController

var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()

override func viewDidLoad() {
    super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {

    let fileURL = NSURL(fileURLWithPath: "PATHTOVIDEO")
    playerView=AVPlayer(url: fileURL as URL)

    playerViewController.showsPlaybackControls=false;

    NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerView.currentItem, queue: nil)
    { notification in
        let t1 = CMTimeMake(1, 100);
        self.playerView.seek(to: t1)
        self.playerView.play()

    }


    playerViewController.player = playerView

    self.present(playerViewController,animated:true){
        self.playerViewController.player?.play()
    }
}

Now what I want to know is how can I swipe between the pages even though the video is playing is there a workaround or something

Alban Gashi
  • 603
  • 4
  • 14
  • I think that the problem is the `swipeRight` gesture recognizer that you add on your second view controller. In my opinion it captures the touch and doesn't make the scrollview handle it. – Franco Meloni Dec 19 '16 at 13:08
  • that part was just to try it out otherwise it doesn't work in either way when the video is playing – Alban Gashi Dec 19 '16 at 13:13
  • 1
    Ok. The problem is that you are presenting the `playerViewController`, then when you are playing the video you are in a new `UIWindow` and you have not anymore the touch on the old window. – Franco Meloni Dec 19 '16 at 13:20
  • 1
    Thank you Franco Meloni, your help combined with the help of the below answer I found out how to do it. – Alban Gashi Dec 19 '16 at 13:45

1 Answers1

3

Try to embed your AVPlayerViewController in your SecondViewController instead of presenting it.

Code Example:

let url = // whatever
let player = AVPlayer(URL:url)
let av = AVPlayerViewController()
av.player = player
av.view.frame = // whatever
self.addChildViewController(av)
self.view.addSubview(av.view)
av.didMoveToParentViewController(self)

Code from: AVPlayer with playback controls of avplayerviewcontroller

Community
  • 1
  • 1