Playing Multi URL Videos from buttons

Hello,

I hope you can help me.



Im a beginner on Xcode using Swift language. I'm trying to build a simple app.

Im struggle to create a button that link to URL videos. The only ONE button is working to play the videos but i can't create more than 1 buttons to play videos.


See the script below:

As you see there are three buttons but they all playing same video... Can you code it that each button can play different URL videos please.


//

// ViewController.swift

//

// Created by Thomas Maher on 22/06/2017.

// Copyright © 2017 Thomas Maher. All rights reserved.

//

import UIKit

import AVFoundation

import AVKit



class ViewController: UIViewController {


let avPlayerViewController = AVPlayerViewController()

var avPlayer:AVPlayer?


override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

let movieUrl:NSURL? = NSURL(string: "http://www.discoverycamp.co.uk/BT_Video/Alphabet.mp4")

if let url = movieUrl {

self.avPlayer = AVPlayer(url: url as URL)

self.avPlayerViewController.player = self.avPlayer

}

}



override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}


@IBAction func Alphabet(_ sender: UIButton) {

self.present(self.avPlayerViewController, animated: true) { () -> Void in

self.avPlayerViewController.player?.play() }

}


@IBAction func Colours(_ sender: UIButton) {

self.present(self.avPlayerViewController, animated: true) { () -> Void in

self.avPlayerViewController.player?.play() }

}

@IBAction func Family(_ sender: UIButton) {

self.present(self.avPlayerViewController, animated: true) { () -> Void in

self.avPlayerViewController.player?.play() }

}


}









As you see there are three buttons but they all playing same video... Can you code it that each button can play different URL videos please.



Hope this make sense,

Looking forward from you.

Thanks

Thomas.

Replies

To make the three buttons each play a different video, you will need to change the current AVPlayerItem being used by the AVPlayer before telling it to play.


     @IBAction func Family(_ sender: UIButton) {
      
        let movieURL = URL(string: "http://www.apple.com/BT_Video/Family.mp4")!
        let avItem = AVPlayerItem(url: movieURL)
        self.avPlayerViewController.player?.replaceCurrentItem(with:avItem)
      
        self.present(self.avPlayerViewController, animated: true) { () -> Void in
            self.avPlayerViewController.player?.play()            }
      
    }


You will need to do the same sort of thing for all three buttons with the correct movie URLs, so that the first one switches back to your Alphabet video too.


I'm not sure how much overhead an AVPlayerItem adds, or how they download videos, so you'll have to figure out whether or not it is better to create all three AVPlayerItem instances for the videos in viewDidLoad() method and then keep them so that you can just assign them to the AVPlayer in each button action, or if it works ok to just create a new AVPlayerItem each time.