Giter VIP home page Giter VIP logo

autovideoplayer's Introduction

AutoVideoPlayer

Play/pause videos automatically in UITableview when an UITableViewCell is in focus, videos can be easily embedded in any UITableViewCell subclass. Can be easily extended to support UICollectionView

  • Easily implement video player in any UITableView subclass
  • Automatic video play when video view is visible and option to easily pause/play any video
  • Mute/Unmute videos
  • Videos are cached in memory and will be removed when there is memory pressure
  • The scroll of UITableView is super smooth since video assets are downloaded on background thread and played only when assets are completely downloaded ensuring the main thead is never blocked
  • Option to provide different bit rate for videos
  • Works when the app comes again from background

It can also be used to play videos in any subclass of UIView.

Demo

Download

Drag and drop the VideoPlayLibrary folder in your project

Usage

Adopt ASAutoPlayVideoLayerContainer protocol in your UITableviewCell subclass like below.

var videoLayer: AVPlayerLayer = AVPlayerLayer()
    
var videoURL: String? {
    didSet {
        if let videoURL = videoURL {
            ASVideoPlayerController.sharedVideoPlayer.setupVideoFor(url: videoURL)
        }
        videoLayer.isHidden = videoURL == nil
    }
}

Implement following method to return the visible height of the UITableViewCell

func visibleVideoHeight() -> CGFloat {
  //return visible height of the Video Player layer
}

ViewController Code

Put following code in viewDidLoad

NotificationCenter.default.addObserver(self,
                                       selector: #selector(self.appEnteredFromBackground),
                                       name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)

Add following code to play/pause when view appears/disappears

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    pausePlayeVideos()
}

Add following methods

@objc func appEnteredFromBackground() {
    ASVideoPlayerController.sharedVideoPlayer.pausePlayeVideosFor(tableView: tableView, appEnteredFromBackground: true)
}

func pausePlayeVideos(){
    ASVideoPlayerController.sharedVideoPlayer.pausePlayeVideosFor(tableView: tableView)
}

Add following code in UITableView delegate and datasource methods

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //if cell adopts ASAutoPlayVideoLayerContainer protocol then
    //set videoURL if you want to show video or else nil
}

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if let videoCell = cell as? ASAutoPlayVideoLayerContainer, videoCell.videoURL != nil {
        ASVideoPlayerController.sharedVideoPlayer.removeLayerFor(cell: videoCell)
    }
}

Add following code to pause/play videos when scroll stops

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if !decelerate {
        pausePlayeVideos()
    }
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    pausePlayeVideos()
}

autovideoplayer's People

Contributors

ashish0309 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

autovideoplayer's Issues

The last cell

The list slides down to the penultimate video, How to play the last video

First time Video playing issue

when we try to play a collection of videos from API URLs, first video never gets played . if we go next video and come back then video starts paly. anyone face the same issue, pls help me

How to add Mute button on Tableview cell ?

I've added one button for Mute - Unmute Video, but not able to do it .

func mutePressed(indexPath: IndexPath) {
guard let cell = feedTbl.cellForRow(at: indexPath) as? HomeVideoCell else {
return
}
cell.playerController?.mute = cell.playerController?.mute ?? false ? false : true
}

Please give me a solution for that.

Not able to play video in Collection View

I am using "AutoVideoPlayer" in my application in which I have added this in the UITableview and it's working. Now I have to also use it UICollectionView cell and it's not working as expected. Kindly help me out with this.
Thanks.

Takes a lot time to load first video

Hey @ashish0309 , A big Thank you for this awesome library but can you please help me with loading a first video as it takes too much time to load or not load at all otherwise it works perfect.

Please Help!

Need video controls

I am using the AutoVideoPlayer in the UITableView. It's working well. Now I also want to add video controls like current and total timer labels, Timer seeking slider. Has anyone implemented this? If, Kindly help on this.
Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.