Swift 中的 100ms Sample iOS 应用
设置
此存储库包含用 Swift 编写的示例代码。
CocoaPods
- 安装 CocoaPods 1.7.5 或更高版本。
- 在此项目根目录中运行
pod install
。CocoaPods 将安装HMSVideo.framework
并设置xcworkspace
。 - 打开
HMSVideoExample.xcworkspace
。
注意:您可能需要通过在终端中运行 pod repo update master
来更新 CocoaPods 的 Master Spec Repo,以便获取 HMSVideo 的最新规范。
快速入门
运行示例应用
要开始使用示例应用,请按照以下步骤操作
- 在Xcode中打开
HMSVideo_Example
目标,从HMSVideoExample.xcworkspace
,然后转到签名与能力
选项卡
-
将版本标识符值替换为您组织独特的值,并选择用于代码签名的团队。
-
打开
MeetingViewController.swift
-
将tokenServerURL常量值替换为正确的URL( 请参阅下面的说明以获取令牌API服务器生成方法)
-
构建并运行应用
令牌生成
在启动100ms客户端时,您需要发送一个由以下元素组合生成的令牌:access_key
、customer_secret
、customer_id
、app_id
、peer_id
和 room_id
- 请按照以下说明操作: https://100ms.gitbook.io/100ms/helpers/runkit
- 发布您的runkit并复制端点URL(单击并复制打开的URL)。
- 将
MeetingViewController.swift
中的tokenServerURL常量值替换为复制的值
100ms iOS (v0.10.0) SDK 文档
在此您可以找到使用100ms iOS SDK构建视频互动体验所需的一切。深入了解我们的SDK、快速入门以及将实时视频、语音和屏幕共享添加到您的Web和移动应用中。
先决条件
- iOS 10.0+
- Xcode 11+
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。有关使用和安装说明,请访问他们的网站。要使用 CocoaPods 将 Brytecam SDK 集成到您的 Xcode 项目中,请在其 Podfile
中指定
pod 'HMSVideo', '~> 0.10.0'
快速开始
查看示例应用:https://github.com/100mslive/hmsvideo-ios/tree/master/Example
概念
- 房间 - 代表实时音频、数据、视频和/或屏幕共享会话,是 Brytecam 视频 SDK 的基本构建块
- 流 - 代表实时音频、视频和数据媒体流,这些流被共享到房间中
- 对等方/参与者 - 代表连接到房间的所有参与者(除本地参与者之外)
- 发布 - 本地参与者可以通过“发布”自己的轨道到房间来共享其音频、视频和数据轨道
- 订阅 - 本地参与者可以通过“订阅”任何对等方的轨道来流式传输任何对等方的音频、视频和数据轨道
- 广播 - 本地参与者可以向房间中的所有对等方发送任何消息/数据
创建和实例化 HMSClient(100ms 客户端)
这将创建一个 HMSClient
对象
//Create an HMSPeer instance for local peer
let peer = HMSPeer(name: userName, authToken: "INSERT TOKEN HERE")
let config = HMSClientConfig()
//config.endpoint = "Override endpoint URL if needed"
//Create a 100ms video client
client = HMSClient(peer: peer, config: config)
点击此处了解如何生成您的令牌:https://www.notion.so/Token-Generation-42b0f9d078224db4bf934608829a8b53
将 wss://prod-in.100ms.live/ws
作为生产环境的端点URL,将 wss://staging-in.100ms.live/ws
作为暂存环境的端点URL
设置监听器
加入后,立即添加监听器以监听其他成员加入和房间的流增加
client.onPeerJoin = { (room, peer) in
// Update UI if needed
}
client.onPeerLeave = { (room, peer) in
// Update UI if needed
}
client.onStreamAdd = { (room, peer, streamInfo) in
// Subscribe to the stream if needed
}
client.onStreamRemove = { (room, peer, streamInfo) in
// Remove remote stream view if needed
}
client.onBroadcast = { (room, peer, message) in
// update UI if needed
}
client.onConnect = {
// Client connected, this is a good place to call join(room)
}
client.onDisconnect = { error in
// Connection lost or could not be established.
// Good place to retry or show an error to the user.
}
连接
实例化 HMSClient
后,连接到100ms服务器
//The client will connect to the WebSocket channel provided through the config
client.connect
加入房间
//Pass the unique id for the room here as a String
let room = HMSRoom(roomId: roomName)
client.join(room) { (success, error) in
//check for error and publish a local stream
}
为每场会话生成一个唯一的 roomid
以避免冲突
创建和获取本地相机/麦克风流
//You can set codec, bitrate, framerate, etc here.
let constraints = HMSMediaStreamConstraints()
constraints.shouldPublishAudio = true
constraints.shouldPublishVideo = true
constraints.codec = .VP8
constraints.bitrate = 256
constraints.frameRate = 25
constraints.resolution = .QVGA
let localStream = client.getLocalStream(constraints)
请使用以下设置以获得精美的明信片大小视频 - 编解码器:VP8
,比特率 256
,帧速率 25
。我们将在未来扩展此设置以包括更多选项,包括前/后置相机
Apple要求您的应用在系统请求摄像头或麦克风权限时向用户显示静态消息
如果您的应用使用设备摄像头,请在其Info.plist文件中包含NSCameraUsageDescription键。
如果您的应用使用设备麦克风,请在其Info.plist文件中包含NSMicrophoneUsageDescription键。
对于每个键,提供一条信息,说明为什么您的应用需要捕获媒体,以便用户可以放心地授予您的应用权限。
重要
如果您的应用在请求授权或尝试使用捕获设备时,其Info.plist文件中缺少相应的键,系统将终止您的应用。
获取屏幕共享的本地媒体
本功能不包含在v0.10 SDK中。即将推出。
显示本地流
//The following code is a sample.
//Get the video capturer and video track
let videoCapturer = stream.videoCapturer
let localVideoTrack = stream.videoTracks?.first
//Begin capturing video from the camera
videoCapturer?.startCapture()
//Create a view for rendering video track and add to the UI hierarchy
if let track = localVideoTrack {
let videoView = HMSVideoView()
videoView.setVideoTrack(track)
view.addSubview(videoView)
}
发布
本地参与者可以通过“发布”其轨道到会议室来共享其音频、视频和数据轨道
client.publish(localStream, room: room, completion: { (stream, error) in
//Handle error if any, update UI if needed
})
订阅
此方法“订阅”了一个对等方的流。理想情况下应在 onStreamAdd
监听器中调用
client.subscribe(streamInfo, room: room, completion: { (stream, error) in
//Handle error if any, update UI if needed
})
广播
此方法将有效载荷广播给所有参与者
client.broadcast(message, room: room, completion: { (stream, error) in
//Handle error if any, update UI if needed
})
取消发布本地流
client.unpublish(stream, room: room, completion: { (stream, error) in
//Handle error if any, update UI if needed
})
取消订阅对等方的流
client.unsubscribe(stream, room: room, completion: { (stream, error) in
//Handle error if any, update UI if needed
})
断开客户端
//The client will disconnect from the WebSocket channel provided
client.disconnect();