Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a local video into a UIView

How to embed a local video into a UIView?

There is a similar question here: Embeding youtube video into a UIView/UIWebView however, it is in obj-c and uses youtube.

like image 443
Hamza Avatar asked Aug 25 '17 16:08

Hamza


People also ask

What is Avplayerlayer?

An object that presents the visual contents of a player object.

What is UIView in Swift?

The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.


1 Answers

You can use 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)
    }
}
like image 66
Abdelahad Darwish Avatar answered Sep 28 '22 14:09

Abdelahad Darwish