GSPlayer Documentation

repository·master·Indexed 19 days ago

https://github.com/wxxsw/gsplayer

A 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.

Tokens
1.4K
Snippets
6
Records
7
Agent score
18%

What's inside GSPlayer

  1. Quick Start with GSPlayer

    master

    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.

    1. Add VideoPlayerView

    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)

    2. Play a Video

    playerView.play(for: someURL)

    3. Control Playback

    // Pause
    if playerView.state == .playing {
        playerView.pause(reason: .userInteraction)
    } 
    
    // Resume
    playerView.resume()

    4. Handle Playback State Changes

    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")
        }
    }
  2. Preload Videos

    master

    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.

    • Set URLs to preload: Use VideoPreloadManager.shared.set(waiting:).
    • Configure preload size: Set 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 * 1024
  3. Use VideoPlayerView API

    master

    The VideoPlayerView is the primary object for managing video playback and visual output.

    Properties

    • 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.

    Callbacks

    • stateDidChanged: Triggered when playback status changes (e.g., play to pause).
    • replay: Triggered when the video reaches the end and is set to replay.

    Methods

    • play(for url: URL): Starts playback for the given URL.
    • pause(reason: PausedReason): Pauses the video.
    • resume(): Continues playback.
  4. Manage Video Cache

    master

    Use VideoCacheManager to interact with the built-in caching mechanism (supports mp4 playback while downloading).

    • Calculate Cache Size: Get the total size of the current video cache.
    • Clean Cache: Remove all cached data.
    // Get total size
    VideoCacheManager.calculateCachedSize()
    
    // Clean all caches
    VideoCacheManager.cleanAllCache()
  5. Reference: VideoPlayerView.PausedReason

    master

    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
    }
  6. Reference: VideoPlayerView.State

    master

    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)
    }