Advertisement
Guest User

AVPlayer example

a guest
May 9th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.04 KB | None | 0 0
  1. import UIKit
  2. import AVFoundation
  3. import AVKit
  4.  
  5. class ViewController: UIViewController {
  6.  
  7.     var player: AVPlayer!
  8.     let videoURL = URL( string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" )
  9.  
  10.     override func viewDidLoad() {
  11.         super.viewDidLoad()
  12.         // Do any additional setup after loading the view.
  13.         videoPlay()
  14.     }
  15.  
  16.     func videoPlay(){
  17.         player = AVPlayer(url: videoURL!)
  18.         let playerController = AVPlayerViewController()
  19.         playerController.player = player
  20.         self.addChild(playerController)
  21.         self.view.addSubview(playerController.view)
  22.         playerController.view.frame = CGRect(x: 0, y: 20, width: 375, height: 207)
  23.         player.play()
  24.         if player.rate != 0 && player.error == nil {
  25.             print("video player is playing.................")
  26.             NotificationCenter.default.addObserver(self,selector: #selector(gettime(_:)),name: nil,object: nil)
  27.         } else {
  28.             print("video player is NOT playing.")
  29.         }
  30.     }
  31.  
  32.     @objc func gettime(_ notification: Notification){
  33.         let currentTime = Float(player.currentTime().value)
  34.         print(currentTime)
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement