Implementing video view in the Storyboard

前端 未结 4 1814
别跟我提以往
别跟我提以往 2021-02-06 13:49

I want to build simple video app thats views a video form youtube links thats users add. I didn\'t find \"VideoView\" I mean If image view is for image what is UIView for video

相关标签:
4条回答
  • 2021-02-06 14:21
    import AVFoundation
    import AVKit
    
    class ViewController: UIViewController { 
        var player = AVPlayer()
        var playerController = AVPlayerViewController()
    
        func playVideo() {     
            let videoURL = NSURL(string: videoUrl)
            player = AVPlayer(url: videoURL! as URL)
            let playerController = AVPlayerViewController()
            playerController.player = player
            self.addChildViewController(playerController)
    
        // Add your view Frame
             playerController.view.frame = self.view.frame
    
       // Add subview in your view
             self.view.addSubview(playerController.view)
    
       player.play()
     }
    
     func stopVideo() {
        player.pause()
     }
    }
    
    0 讨论(0)
  • 2021-02-06 14:25

    In Xcode 10 (with a bit less code):

    1. In the interface builder (Storyboard), add "AVKit Player View Controller"
    2. Create a View Controller that derives from AVPlayerViewController:

      import UIKit
      import AVKit
      
      class VideoPlayerViewController: AVPlayerViewController {
      
        override func viewDidLoad() {
          super.viewDidLoad()
      
          let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")!
          self.player = AVPlayer(url: videoURL)
        }
      
        override func viewDidAppear(_ animated: Bool) {
          self.player?.play()
        }
      }
      
    3. Don't forget to connect this custom class in the storyboard's identity inspector :)

    Note: this example uses video URL pointing to a bundle's resource.

    0 讨论(0)
  • 2021-02-06 14:29

    To play video apple has provided MPMovieViewController see this https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html

    and this

    http://www.brianjcoleman.com/tutorial-play-video-swift/

    In youtube Video case we got embedded links are used so for that you will get help from this https://github.com/gilesvangruisen/Swift-YouTube-Player

    0 讨论(0)
  • 2021-02-06 14:35

    There is no object in the original library that performs video viewing function. But you can import the MediaPlayer framework in your project and add it programmatically.

    Here is a Swift example

    import MediaPlayer
    
    class Step1ViewController: UIViewController {
    
    var moviePlayer: MPMoviePlayerController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        playVideo()
    }
    
    func playVideo() {
    
        let videoView = UIView(frame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.width, self.view.bounds.height))
    
        let pathToEx1 = NSBundle.mainBundle().pathForResource("myVideoFile", ofType: "mp4")
        let pathURL = NSURL.fileURLWithPath(pathToEx1!)
        moviePlayer = MPMoviePlayerController(contentURL: pathURL)
        if let player = moviePlayer {
            player.view.frame = videoView.bounds
            player.prepareToPlay()
            player.scalingMode = .AspectFill
            videoView.addSubview(player.view)
        }
    
        self.view.addSubview(videoView)
    }
    

    }

    As for further customisations and app notifications it has a bunch of in-build possibilities. So check it out.

    0 讨论(0)
提交回复
热议问题