Voltax SDK for iOS
这个库提供了一种简单的方法将 Voltax 播放器集成到原生 iOS 应用中。
实现
将 SDK 添加到您的项目中
CocoaPods
- 设置如下依赖项:
pod 'VoltaxSDK'
- 在终端中执行
pod install
- 打开工作空间文件并运行。
MMVideoView 类
MMVideoView
是 Voltax SDK 的主要类。
创建一个容器视图来容纳 MMVideoView
应该将 MMVideoView
加载到容器视图(UIView)中。这个容器视图可以是 UICollectionView
或 UITableView
的 cell
的 contentView
。
初始化 MMVideoView 实例
MMVideoView
正在被以下属性初始化:
playerId
- 播放器 ID。contentId
- 播放器的内容 ID。articleUrl
(可选)- 文章页面的 URL。
使用以下构造函数中的任何一个来初始化 MMVideoView
的新实例:
let mmVideoView = MMVideoView(playerId: PLAYER_ID, contentId: CONTENT_ID)
let mmVideoView = MMVideoView(playerId: PLAYER_ID, contentId: CONTENT_ID, articleUrl: ARTICLE_URL)
MMVideoViewDelegate
您必须实现 MMVideoViewDelegate
并将其设置为 MMVideoView
的实例。 MMVideoViewDelegate
有一个方法
mmVideoViewHeight(_ height: CGFloat)
- 参见下文“MMVideoView 高度更改”部分。
将委托设置为 MMVideoView
实例
mmVideoView.delegate = self
将 Voltax 播放器加载到 MMVideoView
为了将播放器加载到您的 MMVideoView
实例中,请使用 load()
方法。
@IBOutlet weak var mmVideoViewContainer: UIView!
mmVideoView.load(mmVideoViewContainer)
或者对于 UICollectionView
或 UITableView
,在 willDisplay cell
方法中
mmVideoView.load(cell.contentView)
MMVideoView 高度更改
自定义视图
- 保持容器视图的高度。
- 实现
MMVideoViewDelegate
的mmVideoViewHeight(_ height: CGFloat)
方法 - 使用接口更新容器视图高度。 - 如果您的应用支持方向更改 - 在
viewWillTransition
方法中,调用mmVideoView.viewWillTransition(coordinator)
UICollectionView 项
- 为
MMVideoView
视图高度保持一个变量并将其初始化为 0。
var mmVideoViewCellHeight: CGFloat = 0
- 如以下所示实现
MMVideoViewDelegate
的mmVideoViewHeight(_ height: CGFloat)
方法:
func mmVideoViewHeight(_ height: CGFloat) {
self.mmVideoViewCellHeight = height
self.collectionView.performBatchUpdates(nil, completion: nil)
}
- 在
UICollectionViewDelegateFlowLayout
的sizeForItemAt
方法中,将播放器项大小设置为
CGSize(width: width, height: mmVideoViewCellHeight)
- 在
willDisplay cell
方法中,为MMVideoView
项indexPath
调用self.mmVideoView.willDisplay()
。 - 如果您的应用支持方向更改 - 在
viewWillTransition
方法中,调用mmVideoView.viewWillTransition(coordinator)
并使 collectionView 布局无效。例如
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
collectionView.collectionViewLayout.invalidateLayout()
mmVideoView.viewWillTransition(coordinator)
}
UITableView 项
- 为
MMVideoView
视图高度保持一个变量并将其初始化为 0。
var mmVideoViewCellHeight: CGFloat = 0
- 如以下所示实现
MMVideoViewDelegate
的mmVideoViewHeight(_ height: CGFloat)
方法:
func mmVideoViewHeight(_ height: CGFloat) {
self.mmVideoViewCellHeight = height
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
- 在
UITableViewDelegate
的heightForRowAt
方法中,将播放器项高度设置为self.mmVideoViewCellHeight
。 - 在
willDisplay cell
方法中,为MMVideoView
项indexPath
调用self.mmVideoView.willDisplay()
。 - 如果您的应用支持方向更改 - 在
viewWillTransition
方法中,调用mmVideoView.viewWillTransition(coordinator)
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
mmVideoView.viewWillTransition(coordinator)
}