Swift 语言开发的基于 AVPlayer iOS 播放器,支持横向手势快进、暂停,垂直手势调整亮度及音量,支持全屏,自适应屏幕旋转方向。
// init
self.player = VGPlayer(URL: url)
// or
self.player.replaceVideo(url)
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
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.left.right.equalToSuperview()
make.height.equalTo(strongSelf.view.snp.width).multipliedBy(3.0/4.0) // you can 9.0/16.0
}
VGPlayer 使用 VIMediaCache 实现。
使用 AVAssetResourceLoader 控制 AVPlayer 下载媒体数据。
缓存使用范围请求数据,您可以取消下载,分片缓存
用法
// Settings maxCacheSize
VGPlayerCacheManager.shared.cacheConfig.maxCacheSize = 160000000
// Setting maxCacheAge default one weak
VGPlayerCacheManager.shared.cacheConfig.maxCacheAge = 60 * 60 * 24 * 7
// clean all cache
VGPlayerCacheManager.shared.cleanAllCache()
// clean old disk cache.
// This is an async operation.
VGPlayerCacheManager.shared.cleanOldFiles { }
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
}
self.player.backgroundMode = .proceed
// 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)
下载VGPlayer。移动到您的项目。
CocoaPods
platform :ios, '8.0'
use_frameworks!
pod 'VGPlayer'
github "VeinGuo/VGPlayer"
运行carthage update以构建框架,并将构建好的VGPlayer.framework
和SnapKit.framework
拖到您的Xcode项目中。
MIT