在Storyboard中实现视频视图

时间:2015-09-03 06:48:26

标签: ios swift uiimageview storyboard

我想构建一个简单的视频应用,可以查看用户添加的视频表单。 我没有找到" VideoView"我的意思是如果图像视图是用于图像的视频的UIView。

4 个答案:

答案 0 :(得分:7)

原始库中没有执行视频查看功能的对象。但您可以在项目中导入MediaPlayer框架并以编程方式添加它。

这是一个Swift示例

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

}

至于进一步的自定义和应用程序通知,它有一堆内置的可能性。检查一下。

答案 1 :(得分:3)

要播放视频,Apple已经提供了MPMovieViewController 请参阅此https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html

和这个

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

在youtube视频案例中我们使用了嵌入式链接,因此您可以从https://github.com/gilesvangruisen/Swift-YouTube-Player获得帮助

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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.