Casty 0.3.2

Casty 0.3.2

Amir Riyadh 维护。



Casty 0.3.2

  • amirriyadh

Casty

CI Status Version License Platform Swift

入门

Casty 是一个简单的 swift 库,用于使 Google Cast SDK 的集成变得简单,通过少量代码即可实现,提供所需功能以通过 Google Cast SDK 设置和播放您的媒体。

安装

Casty 可通过 CocoaPods 获取。要安装它,请简单地将以下行添加到您的 Podfile

pod 'google-cast-sdk'
pod 'Casty'

如何使用

首先导入 GoogleCast 和 Casty

import GoogleCast
import Casty

然后在 AppDelegate 类的 didFinishLaunchingWithOptions 中

let appId = "12345678"
Casty.shared.setupCasty(appId: appId, useExpandedControls: true)

现在在 ViewController 类中执行以下操作

  • 初始化 Casty
Casty.shared.initialize()
  • 将 casty 按钮添加到您的 ViewController
let button = Casty.castButton
button?.tintColor = .red
let barButton = UIBarButtonItem(customView: button!)
navigationItem.rightBarButtonItem = barButton
  • 在会话启动时加载媒体
let url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/mp4/DesigningForGoogleCast.mp4"

let subtitleURL = "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/tracks/DesigningForGoogleCast-en.vtt"


let subtitle = GCKMediaTrack(identifier: 1, contentIdentifier: subtitleURL , contentType: "text/vtt", type: GCKMediaTrackType.text, textSubtype: GCKMediaTextTrackSubtype.captions, name: "English", languageCode: "en", customData: nil)

//this image will show up in expanded controller as well as video thumb
let image = GCKImage(url: URL(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/images/780x1200/DesigningForGoogleCast-887x1200.jpg")!, width: 780, height: 1200)

Casty.didStartSession = { _ in
Casty.shared.loadMedia(url: self.url, subtitles: [subtitle], activeSubtitleID: 1, title: "Dev", image: image)
Casty.shared.presentExpandedController()
}

此时,您可以在有活动 Chromecast 设备时看到 cast 按钮图标,当您触摸并连接会话时,视频将开始流式传输。

您可以将默认的迷你控制器添加到您的视图中

Casty.shared.addMiniController(toParentViewController: self)

要定期获取流式传输视频的位置

Casty.enableMediaWatchPosition = true
Casty.mediaWatchPosition = { p in
print("position ----",p)
}

重要提示

  • 请确保您的设备连接的是与 Chromecast 相同的 Wi-Fi 网络。

  • 对于自适应媒体流,Google Cast 需要存在 CORS 头,即使简单的 mp4 媒体流在包含轨道的情况下也需要 CORS。如果您想要为任何媒体启用轨道,您必须同时为您的轨道流和媒体流启用 CORS。因此,如果您在服务器上的简单 mp4 媒体上没有可用的 CORS 头,并且您添加了一个简单的字幕轨道,您将无法进行媒体流除非您更新服务器以包含适当的 CORS 头。

  • 如果您想要高级特性,我建议您在 casty 无需情况下使用 GoogleCast

作者

amirriyadh, [email protected]

许可

Casty 是根据 MIT 许可证可用。有关更多信息,请参阅 LICENSE 文件。-->