1

can anyone please help me, I am trying to add separate buttons to play separate videos in the same view controller but I don't know how to.

this is my code, how do I do this?

import UIKit import AVKit

class ViewController: UIViewController {

@IBAction func Town(_ sender: Any) {

if let path = Bundle.main.path(forResource: "grey", ofType: "mov") {


        let video = AVPlayer(url: URL(fileURLWithPath: path))
        let videoPlayer = AVPlayerViewController()

        videoPlayer.player = video

        self.present(videoPlayer, animated: true, completion: {
            video.play()
        })

}
    func viewDidLoad() {
    super.viewDidLoad()

}

}

}

  • _"Trying to add separate buttons... but I don't know how to."_ Found [this useful thread](https://stackoverflow.com/questions/24030348/how-to-create-a-button-programmatically) in a few seconds using Google search. After you learn how to create a button, use `targetAction` to run a function, where the function sets which video filename to play and also does the `video.play()` part. – VC.One May 10 '20 at 06:46

2 Answers2

0

To play 2 videos simultaneously in same viewController, you need to create 2 separate views and 2 respective buttons in your Storyboard. Remaining functionality will go inside your IBActions. Please use following code:

class VideoPlaybackViewController: UIViewController {

@IBOutlet weak var videoView1: UIView!
@IBOutlet weak var videoView2: UIView!

@IBAction func playFirstVideo(_ sender: Any) {
    guard let path = Bundle.main.path(forResource: "640", ofType: "mov") else {
        print("Video Source Not Found")
        return
    }
    playVideo(playbackURL: URL(fileURLWithPath: path), playerView: videoView1)
}

@IBAction func playSecondVideo(_ sender: Any) {
    guard let path = Bundle.main.path(forResource: "720", ofType: "mov") else {
        print("Video Source Not Found")
        return
    }
    playVideo(playbackURL: URL(fileURLWithPath: path), playerView: videoView2)
}

func playVideo(playbackURL: URL, playerView: UIView) {
    let player = AVPlayer(url: playbackURL)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = playerView.bounds
    playerView.layer.addSublayer(playerLayer)
    player.play()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}
}

Please see the screen with this implementation

grow4gaurav
  • 3,145
  • 1
  • 11
  • 12
0

I used this code and it worked perfectly

import UIKit import AVKit

class ViewController: UIViewController {

@IBOutlet weak var videoView1: UIButton!
@IBOutlet weak var videoView2: UIButton!

@IBAction func playFirstVideo(_ sender: Any) {
      if let path = Bundle.main.path(forResource: "grey", ofType: "mov") {

           let video = AVPlayer(url: URL(fileURLWithPath: path))
           let videoPlayer = AVPlayerViewController()

           videoPlayer.player = video

           self.present(videoPlayer, animated: true, completion: {
               video.play()
           })

    }
}


@IBAction func playSecondVideo(_ sender: Any) {
  if let path = Bundle.main.path(forResource: "go", ofType: "mov") {

       let video = AVPlayer(url: URL(fileURLWithPath: path))
       let videoPlayer = AVPlayerViewController()

       videoPlayer.player = video

       self.present(videoPlayer, animated: true, completion: {
           video.play()
       })
    }
}

}