YTLiveStreaming
YTLiveStreaming 是一个开源的 iOS 框架,使用 YouTube Live Streaming API(YouTube 数据 API v3)在 YouTube 上创建直播和视频流。
要求
- Xcode 13+
- Swift 5.0
简介
-
首先,在您的账户中启用 YouTube LIVE。
-
转到您的 Google 账户 https://console.developers.google.com
-
创建一个新的应用程序。
-
转到新的应用程序
-
选择库
-
选择 "YouTube Data API v3"
-
选择凭据
-
创建 API 密钥(API_KEY);在密钥限制中选择 iOS,输入您的 iOS 应用程序包标识符;保存
-
创建 Oauth 2.0 客户端 ID (CLIENT_ID)
-
为 Google API 添加三个范围:“https://www.googleapis.com/auth/youtube”、“https://www.googleapis.com/auth/youtube.readonly”、“https://www.googleapis.com/auth/youtube.force-ssl”
-
填写应用首页链接和应用隐私政策链接。提交进行验证
-
结果您将获得API_KEY和CLIENT_ID,这些将在您稍后iOS应用的Config.plist中使用。
安装
CocoaPods
CocoaPods 是Cocoa项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
构建YTLiveStreaming需要CocoaPods 1.1.0+
要使用CocoaPods将YTLiveStreaming集成到您的Xcode项目中,请在您的Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!
target '<Your Target Name>' do
pod 'YTLiveStreaming'
end
然后,运行以下命令
$ pod install
Swift Package Manager
要使用Apple的Swift包管理器集成YTLiveStreaming包
- 打开您的Xcode项目
- 选择“文件” - “添加包...
- 在Apple Swift Packages屏幕中选择“搜索或输入包URL”
- 输入 https://github.com/SKrotkih/YTLiveStreaming.git
- 确保YTLiveStreaming已打开
- 按“添加包”
- Xcode将创建包含YTLiveStreaming包的最新版本的“包依赖”组
- 打开您的Xcode项目设置 - 在“包依赖”标签页上的“项目”部分
- 确保YTLiveStreaming包名在那里展示
用户指南
[YouTube Live Streaming API Reference](https://developers.google.com/youtube/v3/live/docs)
import YTLiveStreaming
...
let input: YTLiveStreaming = YTLiveStreaming
...
// Get all events in different arrays of the LiveBroadcastStreamModel type
input.getAllBroadcasts(){ (upcomingEvents, liveNowEvents, completedEvents) in
...
}
// Get events separately:
// Get Ready to broadcast events
input.getUpcomingBroadcasts() { result in
switch result {
case .success(let upcomingEvents):
...
case .failure(let error):
...
}
}
// Get Live now broadcasts
input.getLiveNowBroadcasts() ( result in
switch result {
case .success(let liveNowEvents):
...
case .failure(let error):
...
}
}
// Get Completed broadcasts
input.getCompletedBroadcasts() ( result in
switch result {
case .success(let completedEvents):
...
case .failure(let error):
...
}
}
// Create Broadcast
input.createBroadcast(title, description: description, startTime: startDate, completion: { liveBroadcast in
if let liveBroadcast = liveBroadcast {
...
}
})
// Update of the existing broadcast: LiveBroadcastStreamModel
input.updateBroadcast(broadcast, completion: { success in
if success {
...
}
})
// Start broadcast streaming video
input.startBroadcast(broadcast, delegate: self, completion: { streamName, streamUrl, _ in
if let streamName = streamName, let streamUrl = streamUrl {
completion(streamUrl, streamName)
}
})
// Finish broadcast streaming video
input.completeBroadcast(broadcast, completion: { success in
if success {
...
}
})
// Delete broadcast video from YouTube
input.deleteBroadcast(id: broadcastId, completion: { success in
if success {
...
}
})
以及YTLiveStreaming类的一些其他公开方法
YTLiveStreaming框架使用示例
LiveEvents 是使用框架的示例
使用的库
- SwiftyJSON
- Moya
- KeychainAccess
注意。这里使用了以下内容
- Podspec (https://guides.cocoapods.org.cn/syntax/podspec.html )
- Swiftlint
- Xcode单元测试
- Objective-C + Swift代码示例
项目创建于2016年11月11日
变更历史:2022年12月20日
- 构建版本0.2.29
- 更新项目结构
- 添加方法描述 2022年12月19日
- 构建版本0.2.28
- 更新所有数据结构
- 为字段、请求、响应添加描述 2022年7月25日
- 构建版本0.2.27 2022年7月16日
- 构建版本0.2.26
- 添加了package.swift,可以将框架作为包集成。2022年6月29日从项目中移除示例项目(LiveEvents),并放入单独的仓库中。2022年6月14日
- 除了RxSwift的publisher subject外,还添加了基于Combine的方法来处理Google Sign-in的结果;
- 更新Google Sign-In以符合框架的实际版本;
- 使用SwiftUI实现了Google SignIn界面。2021年5月18日
- 为YouTube视频播放器添加了基于SwiftUI的内容视图。2021年5月4日
- 添加了youtube-ios-player-helper作为视频播放器
- 添加了Xcode单元测试 2021年3月15日
- 添加了Swiftlint
- 修复了Swiftlint警告
- 将示例应用重命名为LiveEvents 2020年4月29日
- 构建版本0.2.17
- 重新设计了示例应用
- GoogleSignIn(在示例应用中使用):更新到5.0.2