如何在ios的小框架中显示视频

时间:2016-11-19 09:29:48

标签: ios swift xcode swift3

我想在视图的上半部分显示一个主题的视频,并在视图的下半部分的textview中显示它的内容。对于那个视频控件,我希望有播放,暂停,停止,ff等功能。此外,我想从本地资源播放它,因为我的网络服务尚未设置。请提出一个很好的解决方案

我已尝试UIWebView并为webview和textview添加了约束,但由于某种原因,网络视图未正确显示视频。下面是我的代码

    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 

2 个答案:

答案 0 :(得分:2)

导入AVFoundationAVKit

然后使用网址对象播放视频(在Swift 3中 NSURL 重命名为网址

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

答案 1 :(得分:1)

您可以使用AVPlayerLayer并给它限制。

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