Implementing video view in the Storyboard

本秂侑毒 提交于 2021-02-17 20:52:29

问题


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 videos.


回答1:


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.




回答2:


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




回答3:


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()
 }
}



回答4:


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.



来源:https://stackoverflow.com/questions/32368751/implementing-video-view-in-the-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!