将本地视频嵌入UIView

时间:2017-08-25 16:59:30

标签: ios swift uiview youtube

如何将本地视频嵌入到UIView中?

这里有一个类似的问题:Embeding youtube video into a UIView/UIWebView 但是,它是obj-c并使用youtube。

1 个答案:

答案 0 :(得分:11)

您可以使用AVPlayer

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {

    var avPlayer: AVPlayer!

    override func viewDidLoad() {

        super.viewDidLoad()
        let filepath: String? = Bundle.main.path(forResource: "qidong", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)


        avPlayer = AVPlayer(url: fileURL)


        let avPlayerController = AVPlayerViewController()
        avPlayerController.player = avPlayer
        avPlayerController.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

//  hide show control
        avPlayerController.showsPlaybackControls = false
// play video

        avPlayerController.player?.play()
        self.view.addSubview(avPlayerController.view)
    }
}