0

I created a simple ViewController class that plays a livestream video using AVPlayer. The livestream is loaded in viewDidLoad and when the user presses play, it plays the livestream, simple enough.

The parent VC is set for portrait orientation only.

I have another button that segues to another ViewController using a custom segue.

How do I pass the AVPlayer and its "state" to the destination ViewController?......i.e. if the AVPlayer is currently playing, it should maintain its state when it's passed from parent VC to destination VC.

FYI: I'm not using prepareForSegue.

Here's the complete simple code:

ViewController.swift:

class ViewController: UIViewController {
    @IBOutlet var playerView: UIView!
    var player = AVPlayer()
    var avPlayerLayer: AVPlayerLayer!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        let url = "http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/appleman.m3u8"  //"http://68.235.37.11:1935/vietmagazine/vietmagazine/playlist.m3u8"

        let playerItem = AVPlayerItem( URL:NSURL( string:url )! )
        player = AVPlayer(playerItem:playerItem)

        avPlayerLayer = AVPlayerLayer(player: player)

        // Add the layer to the view
        playerView.layer.insertSublayer(avPlayerLayer, atIndex: 0)

    }

    @IBAction func playVIdeo(sender: AnyObject)
    {
        avPlayerLayer.player!.play()
    }

    @IBAction func change(sender: AnyObject)
    {
        avPlayerLayer.player!.pause()

        self.performSegueWithIdentifier("CustomSegue", sender: self)
    }

    override func viewWillLayoutSubviews()
    {
        super.viewWillLayoutSubviews()

        // Layout subviews manually
        avPlayerLayer.frame = playerView.bounds   
    }
}

CustomSegue.swift:

class CustomSegue: UIStoryboardSegue
{
    override func perform()
    {
        let sourceVC = self.sourceViewController
        let destinationVC = self.destinationViewController

        sourceVC.view.addSubview(destinationVC.view)

        destinationVC.view.transform = CGAffineTransformMakeScale(0.05, 0.05)

        UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: { () -> Void in

            destinationVC.view.transform = CGAffineTransformMakeScale(1.0, 1.0)

            } ) { (finished) -> Void in

                destinationVC.view.removeFromSuperview()

                let time = dispatch_time(DISPATCH_TIME_NOW, Int64(0.001 * Double(NSEC_PER_SEC)))

                dispatch_after(time, dispatch_get_main_queue(), { () -> Void in

                    sourceVC.presentViewController(destinationVC, animated: false, completion: nil)

                })
        }
    }
}

Thanks.

Pangu
  • 3,721
  • 11
  • 53
  • 120

1 Answers1

0

Would passing the AVPlayer instance be enough to pass the player and its state?

Not tested, but does this work? In the first view controller:

class ViewController: UIViewController {
  var player = AVPlayer()

  @IBAction func myAction(sender: AnyObject) {
    performSegueWithIdentifier("TheSegue", sender: self)
  }
}

class MySegue: UIStoryboardSegue {
  override func perform() {
      let viewController = sourceViewController as! ViewController
      let secondViewController = destinationViewController as! SecondViewController
      secondViewController.player = viewController.player
      sourceViewController.presentViewController(destinationViewController, animated: true, completion: nil)
  }
}

And in the second view controller:

class SecondViewController: UIViewController {
    var player: AVPlayer!

    override func viewDidLoad() {
        // Do something with the player
    }
}

Note, this answer show how to check an AVPlayer;'s state.

Community
  • 1
  • 1
paulvs
  • 11,963
  • 3
  • 41
  • 66