特性
- 完全可定制的 UI。
- 易于使用的 API 和回调。
- 内置缓存机制,支持下载时播放 (mp4)。
- 可以随时预装多个视频。
- 可以嵌入到 UITableView 和 UICollectionView。
- 提供全屏过渡效果。
- 完整演示。
快速开始
- 将
VideoPlayerView
添加到界面中。
let playerView = VideoPlayerView()
view.addSubview(playerView)
// Or in IB, specify the type of custom View as VideoPlayerView.
- 播放视频。
playerView.play(for: someURL)
- 暂停/恢复视频。
if playerView.state == .playing {
playerView.pause(reason: .userInteraction)
} else {
playerView.resume()
}
- 根据播放状态更新控件 UI。
playerView.stateDidChanged = { state in
switch state {
case .none:
print("none")
case .error(let error):
print("error - \(error.localizedDescription)")
case .loading:
print("loading")
case .paused(let playing, let buffering):
print("paused - progress \(Int(playing * 100))% buffering \(Int(buffering * 100))%")
case .playing:
print("playing")
}
}
文档
缓存
获取视频缓存的总大小。
VideoCacheManager.calculateCachedSize()
清除所有缓存。
VideoCacheManager.cleanAllCache()
预加载
设置视频URL为预加载。预加载将自动缓存视频开头的一段内容,并根据当前播放视频的缓冲情况来决定是否开始或暂停预加载。
VideoPreloadManager.shared.set(waiting: [URL])
设置预加载大小,默认值为1024 * 1024,单位是字节。
VideoPlayer.preloadByteCount = 1024 * 1024 // = 1M
全屏
查看演示。
播放器视图
属性
管理播放器的视觉输出的对象。
public let playerLayer: AVPlayerLayer { get }
获取当前视频状态。
public enum State {
/// None
case none
/// From the first load to get the first frame of the video
case loading
/// Playing now
case playing
/// Pause, will be called repeatedly when the buffer progress changes
case paused(playing: Double, buffering: Double)
/// An error occurred and cannot continue playing
case error(NSError)
}
public var state: State { get }
视频暂停的原因。
public enum PausedReason {
/// Pause because the player is not visible, stateDidChanged is not called when the buffer progress changes
case hidden
/// Pause triggered by user interaction, default behavior
case userInteraction
/// Waiting for resource completion buffering
case waitingKeepUp
}
public var pausedReason: PausedReason { get }
重播次数。
public var replayCount: Int { get }
播放进度,取值范围为0-1。
public var playing: Double { get }
已播放时长,单位为秒。
public var currentDuration: Double { get }
缓冲进度,取值范围为0-1。
public var buffering: Double { get }
缓冲时长,单位为秒。
public var currentBufferDuration: Double { get }
视频总时长,单位为秒。
public var totalDuration: Double { get }
该视频总观看时长,单位为秒。
public var watchDuration: Double { get }
视频是否静音,仅针对此实例。
public var isMuted: Bool { get set }
视频音量,仅针对此实例。
public var volume: Double { get set }
回调
播放状态变化,如从播放变为暂停。
public var stateDidChanged: ((State) -> Void)?
播放结束后进行重播。
public var replay: (() -> Void)?
方法
播放指定URL的视频。
func play(for url: URL)
暂停视频。
func pause(reason: PausedReason)
继续播放视频。
func resume()
安装
GSPlayer 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'GSPlayer'
贡献
问题
如果您发现错误或需要帮助,可以 创建问题
拉取请求
我们很高兴接受拉取请求 :D。但请确保它对大多数开发者是需要的,使其易于使用。如果您不确定,请创建一个问题,我们可以在您开始编码之前进行讨论。
许可
MIT 许可证 (MIT)