首页 / 更多教程 / 类似西瓜视频、抖音的自动播放库
类似西瓜视频、抖音的自动播放库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了类似西瓜视频、抖音的自动播放库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3296字,纯文字阅读大概需要5分钟。
内容图文
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
原文:https://www.cnblogs.com/exlo/p/13894937.html
内容总结
以上是互联网集市为您收集整理的类似西瓜视频、抖音的自动播放库全部内容,希望文章能够帮你解决类似西瓜视频、抖音的自动播放库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。