AVPlayer与AVPlayerLayer的封装 – VIMVideoPlayer

VIMVideoPlayer是一个AVplayer类和AVPlayerLayer类的简单封装。

安装步骤

VIMVideoPlayerView
和 
VIMVideoPlayer
添加到你的项目中。

include这个repo作为一个git子模块,或者使用cocoapods:

# Add this to your podfile
target 'MyTarget' do
   pod 'VIMVideoPlayer', ‘{CURRENT_POD_VERSION}’
end

使用方法

创建一个新的 VIMVideoPlayerView
 实例或者设置一个@IBOutlet:

@IBOutlet weak var videoPlayerView: VIMVideoPlayerView!

...

override func viewDidLoad()
{
    // Configure the player as needed
    self.videoPlayerView.player.looping = true
    self.videoPlayerView.player.disableAirplay()
    self.videoPlayerView.setVideoFillMode(AVLayerVideoGravityResizeAspectFill)

    self.videoPlayerView.delegate = self
}

播放一个视频:

// Using an NSURL

if let path = NSBundle.mainBundle().pathForResource("waterfall", ofType: "mp4")
{
    self.videoPlayerView.player.setURL(NSURL(fileURLWithPath: path))
}
else
{
    // Video file not found!
}

/* 
  Note: This must be a URL to an actual video resource (e.g. http://website.com/video.mp4 or .m3u8 etc.),
  It cannot be a URL to a web page (e.g. https://vimeo.com/67069182),
  See below for info on using VIMVideoPlayer to play Vimeo videos.
*/

// Using an AVPlayerItem

let playerItem: AVPlayerItem = ...
self.videoPlayerView.player.setPlayerItem(playerItem)
self.videoPlayerView.player.play()

// Or using an AVAsset

let asset: AVAsset = ...
self.videoPlayerView.player.setAsset(asset)
self.videoPlayerView.player.play()

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:AVPlayer与AVPlayerLayer的封装 – VIMVideoPlayer