2

I'm creating an extremely simple game, and I've hidden the video controls.

@IBAction func playVideo1(_ sender: Any) {

    // play video connected to button 1
    guard let firstVideo = Bundle.main.path(forResource: "Video1", ofType:"mp4") else {
            debugPrint("Video not found")
                return
            }

    // create an AVPlayer, passing it mp4
    let player = AVPlayer(url: URL(fileURLWithPath: firstVideo))


    // Create a new AVPlayerViewController and pass it a reference to the player.
    let controller = AVPlayerViewController()
    controller.player = player

    controller.showsPlaybackControls = false



    // Modally present the player and call the player's play() method when complete.
    present(controller, animated: true) {
        player.play()
    } 
} // end playVideo1

One of the two options would be OK.

Option 1: Tap to close the video.

Option 2: Have the AVPlayer close automatically at the end of the video.

I appreciate any help.

Thanks!

a.masri
  • 2,439
  • 1
  • 14
  • 32
user3444710
  • 587
  • 1
  • 6
  • 13

3 Answers3

3

You can add a tap gesture recognizer to AVPlayerViewController's view (for closing on tap), or you could subscribe to AVPlayerItemDidPlayToEndTime notification (for closing when video ends playing). Something like this:

@IBAction func playVideo1(_ sender: Any) {

    // play video connected to button 1
    guard let firstVideo = Bundle.main.path(forResource: "Video1", ofType:"mp4") else {
            debugPrint("Video not found")
                return
            }

    // create an AVPlayer, passing it mp4
    let player = AVPlayer(url: URL(fileURLWithPath: firstVideo))


    // Create a new AVPlayerViewController and pass it a reference to the player.
    let controller = AVPlayerViewController()
    controller.player = player

    controller.showsPlaybackControls = false

    //for closing when video ends
    NotificationCenter.default.addObserver(self, selector: #selector(closePlayer), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: controller.player?.currentItem)

    //for closing on tap
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(closePlayer))
    controller.view.addGestureRecognizer(tapGestureRecognizer)

    // Modally present the player and call the player's play() method when complete.
    present(controller, animated: true) {
        player.play()
    }
} // end playVideo1

@objc func closePlayer() {
    dismiss(animated: true)
    //if you go notification route, don't forget to remove observer
    NotificationCenter.default.removeObserver(self)
}
Predrag Samardzic
  • 2,661
  • 15
  • 18
2

For closing the videoPlayer when on user tap you can use the following-

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(closePlayerOnTouch))
    controller.view.addGestureRecognizer(tapGestureRecognizer)

and then add this after the button -

@objc func closePlayerOnTouch() {
    dismiss(animated: true)
    NotificationCenter.default.removeObserver(self)
}

@Predrag's answer is really awesome though.

Ayush Verma
  • 169
  • 1
  • 7
0
import UIKit
import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var btnPlay: UIButton!

var player:AVPlayer?

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func btnPress(sender: AnyObject) {
    if (btnPlay.titleLabel?.text == "Play") {
        initPlayer()
        btnPlay.setTitle("Stop", forState: UIControlState.Normal)
    } else {
        stopPlayer()
        btnPlay.setTitle("Play", forState: UIControlState.Normal)
    }
}

func initPlayer()  {
    if let play = player {
        print("playing")
        play.play()
    } else {
        print("player allocated")
        player = AVPlayer(URL: NSURL(string: "http://streaming.radio.rtl.fr/rtl-1-48-192")!)
        print("playing")
        player!.play()
    }
}

func stopPlayer() {
    if let play = player {
        print("stopped")
        play.pause()
        player = nil
        print("player deallocated")
    } else {
        print("player was already deallocated")
    }
}
}
McDonal_11
  • 3,935
  • 6
  • 24
  • 55