Swift开源:VGPlayer - 视频播放器

Swift   2017-06-15 08:57:58 发布
您的评价:
     
0.0
收藏     0收藏
文件夹
标签
(多个标签用逗号分隔)

基于AVPlayer开发的iOS播放器,支持水平手势快进,暂停,垂直手势支持亮度和音量调节,支持全屏,自适应屏幕旋转方向。

示范

需求

  • Swift 3
  • iOS 8.0+
  • XCode 8

特点

  • 支持本地和网络
  • 背景播放模式。
  • 手势调节音量和亮度以及快进和快退。
  • 支持全屏
  • 向前滑动快速
  • 锁屏也可以全屏旋转
  • 支持重播媒体
  • 支持定制播放器视图

TODO

  • Media Cache
  • Virtual reality
  • Subtitle

使用

Play Video

// init 
self.player = VGPlayer(URL: url)
// or
self.player.replaceVideo(url)

Custom player view

  • Subclass VGPlayerView
  • Alloc VGPlaye when set up
let playeView = VGCustomPlayerView()
self.player = VGPlayer(playerView: playeView)

// customPlayerView
class VGCustomPlayerView: VGPlayerView {
    var playRate: Float = 1.0
    var rateButton = UIButton(type: .custom)
    
    override func configurationUI() {
        super.configurationUI()
        self.topView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.09701412671)
        self.bottomView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.09701412671)
        self.topView.addSubview(rateButton)
        rateButton.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.right).offset(10)
            make.centerY.equalTo(closeButton)
        }
        rateButton.tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        rateButton.setTitle("x1.0", for: .normal)
        rateButton.titleLabel?.font   = UIFont.boldSystemFont(ofSize: 12.0)
        rateButton.addTarget(self, action: #selector(onRateButton), for: .touchUpInside)
        rateButton.isHidden = false
    }

// .....more

AutoLayout use SnapKit

self.player.replaceVideo(url)
        view.addSubview(self.player.displayView)
        
        self.player.play()
        self.player.backgroundMode = .proceed
        self.player.delegate = self
        self.player.displayView.delegate = self
        self.player.displayView.titleLabel.text = "China NO.1"
        self.player.displayView.snp.makeConstraints { [weak self] (make) in
            guard let strongSelf = self else { return }
            make.top.equalTo(strongSelf.view.snp.top)
            make.left.equalTo(strongSelf.view.snp.left)
            make.right.equalTo(strongSelf.view.snp.right)
            make.height.equalTo(strongSelf.view.snp.width).multipliedBy(3.0/4.0) // you can 9.0/16.0
        }

Background playback

  • Project setting

  • AppDelegate settings
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        do
        {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        }
        catch let error as NSError
        {
            print(error)
        }
        return true
    }
  • VGPlayer Background playback mode to proceed
self.player.backgroundMode = .proceed

Delegate methods optional

// player delegate
    // play state
    func vgPlayer(_ player: VGPlayer, stateDidChange state: VGPlayerState)
    // playe Duration
    func vgPlayer(_ player: VGPlayer, playerDurationDidChange currentDuration: TimeInterval, totalDuration: TimeInterval)
    // buffer state
    func vgPlayer(_ player: VGPlayer, bufferStateDidChange state: VGPlayerBufferstate)
    // buffered Duration
    func vgPlayer(_ player: VGPlayer, bufferedDidChange bufferedDuration: TimeInterval, totalDuration: TimeInterval)
    // play error
    func vgPlayer(_ player: VGPlayer, playerFailed error: VGPlayerError)
    
    
// player view delegate
    /// fullscreen
    func vgPlayerView(_ playerView: VGPlayerView, willFullscreen fullscreen: Bool)
    /// close play view
    func vgPlayerView(didTappedClose playerView: VGPlayerView)
    /// displaye control
    func vgPlayerView(didDisplayControl playerView: VGPlayerView)

安装

  • Download VGPlayer. Move to your project.

  • Cocoapods

platform :ios, '8.0'
use_frameworks!
pod 'VGPlayer'

参考

许可

MIT

项目主页:http://www.open-open.com/lib/view/home/1497488278534

 

扩展阅读

iOS、mac开源项目及库汇总
iOS及Mac开源项目和学习资料【超级全面】
swift 学习资源 大集合
Swift 开源项目精选
「参与 Swift 开源」(一)—— 在 Linux 上编译、测试 Swift

为您推荐

ASP.NET MVC快速分页的实现
Rapid Web Application Development With Meteor
jade模版引擎
Laravel大型项目系列教程
选择一个 Python Web 框架:Django vs Flask vs Pyramid

更多

Swift
iOS开发
相关文档  — 更多
相关经验  — 更多
相关讨论  — 更多