Install GSPlayer via CocoaPods
masterAdd the following line to your Podfile to install GSPlayer:
pod 'GSPlayer'repository·master·Indexed 19 days ago
https://github.com/wxxsw/gsplayerA customizable Swift video player for iOS and macOS featuring built-in mp4 caching, video preloading, and integration with UITableView and UICollectionView. It provides the VideoPlayerView for playback management, VideoCacheManager for cache control, and VideoPreloadManager for preloading video segments.
Add the following line to your Podfile to install GSPlayer:
pod 'GSPlayer'To get started with GSPlayer, add a VideoPlayerView to your interface, play a video using a URL, and monitor playback status via the stateDidChanged callback.
You can add the view programmatically or via Interface Builder (IB) by specifying VideoPlayerView as the custom view type.
let playerView = VideoPlayerView()
view.addSubview(playerView)playerView.play(for: someURL)// Pause
if playerView.state == .playing {
playerView.pause(reason: .userInteraction)
}
// Resume
playerView.resume()Use the stateDidChanged closure to update your UI based on the current State.
let playerView = VideoPlayerView()
view.addSubview(playerView)
playerView.play(for: someURL)
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")
}
}GSPlayer allows preloading multiple videos. Preloading automatically caches a short segment of the beginning of the video. The system decides whether to start or pause preloading based on the buffering status of the currently playing video.
VideoPreloadManager.shared.set(waiting:).VideoPlayer.preloadByteCount (default is 1024 * 1024 bytes/1MB).// Preload specific URLs
VideoPreloadManager.shared.set(waiting: [url1, url2])
// Set preload size to 1MB
VideoPlayer.preloadByteCount = 1024 * 1024The VideoPlayerView is the primary object for managing video playback and visual output.
playerLayer: The AVPlayerLayer managing visual output.state: The current State of the player.pausedReason: The PausedReason if the player is currently paused.replayCount: Number of times the video has replayed.playing: Current playback progress (0.0 to 1.0).currentDuration: Played length in seconds.totalDuration: Total video duration in seconds.buffering: Buffered progress (0.0 to 1.0).currentBufferDuration: Buffered length in seconds.watchDuration: Total watch time in seconds.isMuted: Mute status.volume: Video volume.stateDidChanged: Triggered when playback status changes (e.g., play to pause).replay: Triggered when the video reaches the end and is set to replay.play(for url: URL): Starts playback for the given URL.pause(reason: PausedReason): Pauses the video.resume(): Continues playback.Use VideoCacheManager to interact with the built-in caching mechanism (supports mp4 playback while downloading).
// Get total size
VideoCacheManager.calculateCachedSize()
// Clean all caches
VideoCacheManager.cleanAllCache()The PausedReason enum describes why the video was paused.
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
}The State enum represents the current playback status of the VideoPlayerView.
public enum State {
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)
}