Phani
Phani

Reputation: 9

how to show video in a small frame in ios

I want to show a video of a topic in top half of the view and its matter in textview in the bottom half of the view. For that video control i want to have the features like play,pause,stop,ff etc. Also i want to play it from local resource as my web services hasn't been setup yet. pls suggest a good solution

I have tried UIWebView and added constraints to webview and textview but for some reason the web view is not showing the video correctly. below is my code

    let purl = NSURL(fileURLWithPath: "/Users/Rohit/Desktop/videos/demo/demo/video1.mp4")        webView.loadHTMLString("<iframe width = \" \(webView.frame.width) \" height = \"\(webView.frame.height)\" src = \"\(purl)\"></iframe>", baseURL: nil)
    webView.backgroundColor = UIColor.green
    webView.mediaPlaybackRequiresUserAction = true
    webView.scrollView.isScrollEnabled = true
    webView.isUserInteractionEnabled = true 

Upvotes: 0

Views: 2850

Answers (2)

SwiftStudier
SwiftStudier

Reputation: 2324

Import AVFoundation and AVKit

Then play the video using an URL object (in Swift 3 NSURL is renamed to URL)

let player = AVPlayer(URL: URI)
let controller = AVPlayerViewController()
controller.player = player
self.addChildViewController(controller)
let screenSize = UIScreen.main.bounds.size
let videoFrame = CGRect(x: 0, y: 10, width: screenSize.width, height: (screenSize.height - 10) * 0.5)
controller.view.frame = videoFrame
self.view.addSubview(controller.view)
player.play()

Upvotes: 3

Munahil
Munahil

Reputation: 2419

You can use AVPlayerLayer and give it bounds.

private func inits() {

        //let rootLayer: CALayer = self.layer
       // rootLayer.masksToBounds = true

        avPlayerLayer = AVPlayerLayer(player: player)
        avPlayerLayer.bounds = self.bounds
       // avPlayerLayer.backgroundColor = UIColor.yellowColor().CGColor
        self.layer.insertSublayer(avPlayerLayer, atIndex: 0)
    }

Upvotes: 1

Related Questions