……

类似西瓜视频、抖音的自动播放库

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

当UITableView单元处于焦点中时,在UITableView中自动播放/暂停视频,视频可以很容易地嵌入到任何UITableView单元子类中。
可以轻松扩展支持UICollectionView

  • Easily implement video player in any UITableView subclass

    在任何UITableView子类中轻松实现视频播放器

  • 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

    UITable View的滚动是非常平滑的,因为视频资产是在后台线程上下载的,只有当资产完全下载时才会播放,以确保主thead永远不会被阻塞

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

它还可以用于在UIView的任何子类中播放视频。

Demo

Download

Drag and drop the VideoPlayLibrary folder in your project

拖放项目中的视频播放库文件夹到你的项目中

Usage

Adopt ASAutoPlayVideoLayerContainer protocol in your UITableviewCell subclass like below.

在您的UITableview单元子类中采用ASAuto播放视频层容器协议,如下所示

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

实现以下方法以返回UITableView单元的可见高度

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

ViewController Code

Put following code in viewDidLoad

把下面的代码放在视图DidLoad中

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

在UITableView委托和数据源方法中添加以下代码

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

当滚动停止时,添加以下代码以暂停/播放视频

最后奉上git地址:https://github.com/ashish0309/AutoVideoPlayer

posted on 2020-10-29 09:24  Exlo  阅读(465)  评论(0编辑  收藏  举报

导航