2015-09-03 66 views
3

我想構建簡單的視頻應用程序,它可以查看用戶添加的視頻形式的YouTube鏈接。 我沒有找到「VideoView」我的意思是如果圖像視圖是圖像什麼是UIView的視頻。實現故事板中的視頻視圖

+0

使用[MPMoviePlayerController](https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/)。 –

回答

5

原始庫中沒有執行視頻查看功能的對象。但是您可以在您的項目中導入MediaPlayer框架並以編程方式添加它。

這裏是一個例子斯威夫特

import MediaPlayer 

class Step1ViewController: UIViewController { 

var moviePlayer: MPMoviePlayerController? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    playVideo() 
} 

func playVideo() { 

    let videoView = UIView(frame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.width, self.view.bounds.height)) 

    let pathToEx1 = NSBundle.mainBundle().pathForResource("myVideoFile", ofType: "mp4") 
    let pathURL = NSURL.fileURLWithPath(pathToEx1!) 
    moviePlayer = MPMoviePlayerController(contentURL: pathURL) 
    if let player = moviePlayer { 
     player.view.frame = videoView.bounds 
     player.prepareToPlay() 
     player.scalingMode = .AspectFill 
     videoView.addSubview(player.view) 
    } 

    self.view.addSubview(videoView) 
} 

}

至於進一步的定製和應用通知它有一堆在-建立的可能性。所以檢查一下。

+0

僅供參考此MPMoviePlayerController已棄用 – Xav

2
import AVFoundation 

import AVKit 

class ViewController: UIViewController { 

var player = AVPlayer() 

var playerController = AVPlayerViewController() 

func playVideo() { 

let videoURL = NSURL(string: videoUrl) 
    player = AVPlayer(url: videoURL! as URL) 
    let playerController = AVPlayerViewController() 
    playerController.player = player 
    self.addChildViewController(playerController) 

**Add your view Frame** 
    playerController.view.frame = self.view.frame 

**Add sub view in your view** 
    self.view.addSubview(playerController.view) 

player.play() 

}

func stopVideo() { 

    player.pause() 



} 

}