2016-11-19 81 views
0

我想在視圖的下半部分的視圖的上半部分以及其文本視圖中顯示主題的視頻。對於那個視頻控制,我希望具有播放,暫停,停止,ff等功能。另外,我想從本地資源播放它,因爲我的Web服務尚未安裝。請提出一個很好的解決方案如何在ios中的一個小框架中顯示視頻

我試過UIWebView並增加了對webview和textview的約束,但由於某種原因web視圖沒有正確顯示視頻。以下是我的代碼

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 

回答

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

導入AVFoundationAVKit

然後播放使用URL對象的視頻(在斯威夫特3 NSURL更名爲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()