Swift开源:VGPlayer - 视频播放器
hutyone
7年前
<p style="text-align:center"><img src="https://simg.open-open.com/show/44d30b89190ae2ff19eb0082d12affdf.png"></p> <p>基于AVPlayer开发的iOS播放器,支持水平手势快进,暂停,垂直手势支持亮度和音量调节,支持全屏,自适应屏幕旋转方向。</p> <h2>示范</h2> <p><img src="https://simg.open-open.com/show/768a85df51d45e5f5948fbdd63929034.gif"></p> <p><img src="https://simg.open-open.com/show/48f404810c23caf904b0ac96e98ff7b0.gif"></p> <h2>需求</h2> <ul> <li>Swift 3</li> <li>iOS 8.0+</li> <li>XCode 8</li> </ul> <h2>特点</h2> <ul> <li>支持本地和网络</li> <li>背景播放模式。</li> <li>手势调节音量和亮度以及快进和快退。</li> <li>支持全屏</li> <li>向前滑动快速</li> <li>锁屏也可以全屏旋转</li> <li>支持重播媒体</li> <li>支持定制播放器视图</li> </ul> <h2>TODO</h2> <ul> <li>Media Cache</li> <li>Virtual reality</li> <li>Subtitle</li> </ul> <h2>使用</h2> <h3>Play Video</h3> <pre> <code class="language-swift">// init self.player = VGPlayer(URL: url) // or self.player.replaceVideo(url)</code></pre> <h3>Custom player view</h3> <ul> <li>Subclass VGPlayerView</li> <li>Alloc VGPlaye when set up</li> </ul> <pre> <code class="language-swift">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 </code></pre> <h3>AutoLayout use <a href="/misc/goto?guid=4958968153529640282" rel="nofollow,noindex">SnapKit</a></h3> <pre> <code class="language-swift">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 }</code></pre> <h3>Background playback</h3> <ul> <li>Project setting</li> </ul> <p><img src="https://simg.open-open.com/show/8f8fc673a972d984fc328c4188fed313.png"></p> <ul> <li>AppDelegate settings</li> </ul> <pre> <code class="language-swift">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 }</code></pre> <ul> <li>VGPlayer Background playback mode to proceed</li> </ul> <pre> <code class="language-swift">self.player.backgroundMode = .proceed</code></pre> <h3>Delegate methods optional</h3> <pre> <code class="language-swift">// 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)</code></pre> <h2>安装</h2> <ul> <li> <p>Download VGPlayer. Move to your project.</p> </li> <li> <p>Cocoapods</p> </li> </ul> <pre> <code class="language-swift">platform :ios, '8.0' use_frameworks! pod 'VGPlayer'</code></pre> <h2>参考</h2> <ul> <li><a href="/misc/goto?guid=4959749918876219400" rel="nofollow,noindex">https://techblog.toutiao.com/2017/03/28/fullscreen/</a></li> <li><a href="/misc/goto?guid=4959749918959214835" rel="nofollow,noindex">https://developer.apple.com/library/content/qa/qa1668/_index.html</a></li> <li><a href="/misc/goto?guid=4959749919040715073" rel="nofollow,noindex">https://developer.apple.com/documentation/avfoundation</a></li> <li><a href="/misc/goto?guid=4959749919127554077" rel="nofollow,noindex">https://stackoverflow.com/questions/808503/uibutton-making-the-hit-area-larger-than-the-default-hit-area/13977921</a></li> <li><a href="/misc/goto?guid=4959749919211721570" rel="nofollow,noindex">https://gist.github.com/onevcat/2d1ceff1c657591eebde</a></li> </ul> <h2>许可</h2> <p>MIT</p> <p>项目主页:<a href="http://www.open-open.com/lib/view/home/1497488278534">http://www.open-open.com/lib/view/home/1497488278534</a></p> <p> </p>