VoltaxSDK 1.3.0

VoltaxSDK 1.3.0

Alon ShprungOded Regev 维护。



VoltaxSDK 1.3.0

  • 作者
  • Boris Kalim

Voltax SDK for iOS

这个库提供了一种简单的方法将 Voltax 播放器集成到原生 iOS 应用中。

实现

将 SDK 添加到您的项目中

CocoaPods

  • 设置如下依赖项:pod 'VoltaxSDK'
  • 在终端中执行 pod install
  • 打开工作空间文件并运行。

MMVideoView 类

MMVideoView 是 Voltax SDK 的主要类。

创建一个容器视图来容纳 MMVideoView

应该将 MMVideoView 加载到容器视图(UIView)中。这个容器视图可以是 UICollectionViewUITableViewcellcontentView

初始化 MMVideoView 实例

MMVideoView 正在被以下属性初始化:

  1. playerId - 播放器 ID。
  2. contentId - 播放器的内容 ID。
  3. 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)

或者对于 UICollectionViewUITableView,在 willDisplay cell 方法中

mmVideoView.load(cell.contentView)

MMVideoView 高度更改

自定义视图

  1. 保持容器视图的高度。
  2. 实现 MMVideoViewDelegatemmVideoViewHeight(_ height: CGFloat) 方法 - 使用接口更新容器视图高度。
  3. 如果您的应用支持方向更改 - 在 viewWillTransition 方法中,调用 mmVideoView.viewWillTransition(coordinator)

UICollectionView 项

  1. MMVideoView 视图高度保持一个变量并将其初始化为 0。
var mmVideoViewCellHeight: CGFloat = 0
  1. 如以下所示实现 MMVideoViewDelegatemmVideoViewHeight(_ height: CGFloat) 方法:
func mmVideoViewHeight(_ height: CGFloat) {
    self.mmVideoViewCellHeight = height
    self.collectionView.performBatchUpdates(nil, completion: nil)
}
  1. UICollectionViewDelegateFlowLayoutsizeForItemAt 方法中,将播放器项大小设置为
CGSize(width: width, height: mmVideoViewCellHeight)
  1. willDisplay cell 方法中,为 MMVideoViewindexPath 调用 self.mmVideoView.willDisplay()
  2. 如果您的应用支持方向更改 - 在 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 项

  1. MMVideoView 视图高度保持一个变量并将其初始化为 0。
var mmVideoViewCellHeight: CGFloat = 0
  1. 如以下所示实现 MMVideoViewDelegatemmVideoViewHeight(_ height: CGFloat) 方法:
func mmVideoViewHeight(_ height: CGFloat) {
    self.mmVideoViewCellHeight = height
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}
  1. UITableViewDelegateheightForRowAt 方法中,将播放器项高度设置为 self.mmVideoViewCellHeight
  2. willDisplay cell 方法中,为 MMVideoViewindexPath 调用 self.mmVideoView.willDisplay()
  3. 如果您的应用支持方向更改 - 在 viewWillTransition 方法中,调用 mmVideoView.viewWillTransition(coordinator)
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    mmVideoView.viewWillTransition(coordinator)
}