Skip to content

Repository files navigation

Carousell Group copy. Derived from wxxsw/GSPlayer (MIT), with one patch on top of upstream master: "Use existing player or create a new AVPlayer instance". Consume it by tag, never by branch:

pod'GSPlayer',:git=>'git@github.com:carousell/GSPlayer.git',:tag=>'0.2.30-ct.1'

To pull upstream changes: git remote add upstream https://github.com/wxxsw/GSPlayer.git && git fetch upstream && git merge upstream/master, then cut a new 0.x.y-ct.n tag.

GSPlayer

Features

  • Fully customizable UI.
  • Easy to use API and callbacks.
  • Built-in caching mechanism to support playback while downloading (mp4).
  • Can preload multiple videos at any time.
  • Can be embedded into UITableView and UICollectionView.
  • Provide full screen transition.
  • Complete Demo.

Quick Start

  1. Add VideoPlayerView to the interface.
letplayerView=VideoPlayerView()
view.addSubview(playerView)
// Or in IB, specify the type of custom View as VideoPlayerView.
  1. Play Video.
playerView.play(for: someURL)
  1. Pause/Resume Video.
if playerView.state ==.playing {
playerView.pause(reason:.userInteraction)}else{
playerView.resume()}
  1. Update control UI based on playback status.
playerView.stateDidChanged ={ state inswitch 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")}}

Documents

Cache

Get the total size of the video cache.

VideoCacheManager.calculateCachedSize()

Clean up all caches.

VideoCacheManager.cleanAllCache()

Preload

Set the video URL to be preloaded. Preloading will automatically cache a short segment of the beginning of the video and decide whether to start or pause the preload based on the buffering of the currently playing video.

VideoPreloadManager.shared.set(waiting:[URL])

Set the preload size, the default value is 1024 * 1024, unit is byte.

VideoPlayer.preloadByteCount =1024*1024 // = 1M

Fullscreen

See demo.

PlayerView

Property

An object that manages a player's visual output.

publicletplayerLayer:AVPlayerLayer{get}

Get current video status.

publicenumState{
/// 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)}publicvarstate:State{get}

The reason the video was paused.

publicenumPausedReason{
/// 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
}publicvarpausedReason:PausedReason{get}

Number of replays.

publicvarreplayCount:Int{get}

Played progress, value range 0-1.

publicvarplaying:Double{get}

Played length in seconds.

publicvarcurrentDuration:Double{get}

Buffered progress, value range 0-1.

publicvarbuffering:Double{get}

Buffered length in seconds.

publicvarcurrentBufferDuration:Double{get}

Total video duration in seconds.

publicvartotalDuration:Double{get}

The total watch time of this video, in seconds.

publicvarwatchDuration:Double{get}

Whether the video is muted, only for this instance.

publicvarisMuted:Bool{getset}

Video volume, only for this instance.

publicvarvolume:Double{getset}

Callback

Playback status changes, such as from play to pause.

publicvarstateDidChanged:((State)->Void)?

Replay after playing to the end.

publicvarreplay:(()->Void)?

Method

Play a video of the specified url.

func play(for url:URL)

Pause video.

func pause(reason:PausedReason)

Continue playing video.

func resume()

Installation

GSPlayer is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod'GSPlayer'

Contribution

Issue

If you find a bug or need a help, you can create a issue

Pull Request

We are happy to accept pull requests :D. But please make sure it's needed by most developers and make it simple to use. If you are not sure, create an issue and we can discuss it before you get to coding.

License

The MIT License (MIT)

About

Vendored fork of wxxsw/GSPlayer (MIT) for Carousell Group iOS apps

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages