-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathAudioCallingViewController.swift
More file actions
500 lines (420 loc) · 15.1 KB
/
AudioCallingViewController.swift
File metadata and controls
500 lines (420 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//
// AudioCallingViewController.swift
// TRTC-API-Example-Swift
//
// Created by peteryhchen on 2022/6/23.
// Copyright © 2022 Tencent. All rights reserved.
//
import UIKit
import TXLiteAVSDK_TRTC
/*
Real-Time Audio Call
TRTC Audio Call
This document shows how to integrate the real-time audio call feature.
1. Switch between the speaker and receiver: trtcCloud.getDeviceManager().setAudioRoute(.speakerphone)
2. Mute the device so that others won’t hear the audio of the device: trtcCloud.muteLocalAudio(true)
3. Display other network and volume information: delegate -> onNetworkQuality, onUserVoiceVolume
Documentation: https://cloud.tencent.com/document/product/647/42046
*/
class UserBox: UIView {
let userPortrait: UIImageView = {
var imageV = UIImageView(image: UIImage(named: "audiocall_user_portrait"))
imageV.backgroundColor = .white
return imageV
}()
let label: UILabel = {
var label = UILabel()
label.backgroundColor = .clear
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 15)
label.textAlignment = .center
return label
}()
override init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: 80, height: 90))
contentMode = .scaleToFill
semanticContentAttribute = .unspecified
isUserInteractionEnabled = true
backgroundColor = .clear
alpha = 1
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private var isViewReady = false
override func didMoveToWindow() {
super.didMoveToWindow()
guard !isViewReady else {
return
}
isViewReady = true
constructViewHierarchy()
activateConstraints()
}
private func constructViewHierarchy() {
addSubview(userPortrait)
addSubview(label)
}
private func activateConstraints() {
userPortrait.snp.makeConstraints{ make in
make.top.equalTo(5)
make.width.equalTo(50)
make.height.equalTo(50)
make.centerX.equalToSuperview()
}
label.snp.makeConstraints{ make in
make.bottom.equalTo(-5)
make.width.equalTo(75)
make.height.equalTo(20)
make.centerX.equalToSuperview()
}
}
}
class CustomRemoteInfo {
var volume: UInt?
var quality: TRTCQuality?
}
class AudioCallingViewController : UIViewController {
let navgationTitle: String = Localize("TRTC-API-Example.AudioCalling.Title")
var roomId: UInt32 = 0
var userId: String = ""
let maxRemoteUserNum = 6
let SDKAppID: UInt32 = 0
let userBox1: UserBox = {
var box = UserBox()
box.isHidden = true
return box
}()
let userBox2: UserBox = {
var box = UserBox()
box.isHidden = true
return box
}()
let userBox3: UserBox = {
var box = UserBox()
box.isHidden = true
return box
}()
let userBox4: UserBox = {
var box = UserBox()
box.isHidden = true
return box
}()
let userBox5: UserBox = {
var box = UserBox()
box.isHidden = true
return box
}()
let userBox6: UserBox = {
var box = UserBox()
box.isHidden = true
return box
}()
lazy var userIdArray: [String] = {
var array = [String]()
return array
}()
let stackView1: UIStackView = {
var view = UIStackView(frame: .zero)
view.backgroundColor = .clear
view.axis = .horizontal
view.alignment = .fill
view.distribution = .fillEqually
view.spacing = 30
return view
}()
let stackView2: UIStackView = {
var view = UIStackView(frame: .zero)
view.backgroundColor = .clear
view.axis = .horizontal
view.alignment = .fill
view.distribution = .fillEqually
view.spacing = 30
return view
}()
let userStatusTableView: UITableView = {
let tableView = UITableView(frame: .zero)
tableView.backgroundColor = .gray
return tableView
}()
let hansFreeButton: UIButton = {
let button = UIButton(frame: .zero)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(Localize("TRTC-API-Example.AudioCalling.speaker"), for: .normal)
button.setTitle(Localize("TRTC-API-Example.AudioCalling.earPhone"), for: .selected)
button.titleLabel?.font = .systemFont(ofSize: 15.0)
button.backgroundColor = UIColor(red: 52.0/255, green: 184.0/255, blue: 97.0/255, alpha: 1)
button.layer.cornerRadius = 8
button.layer.masksToBounds = true
return button
}()
let muteButton: UIButton = {
let button = UIButton(frame: .zero)
button.backgroundColor = UIColor(red: 52.0/255, green: 184.0/255, blue: 97.0/255, alpha: 1)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(Localize("TRTC-API-Example.AudioCalling.mute"), for: .normal)
button.setTitle(Localize("TRTC-API-Example.AudioCalling.cancelMute"), for: .selected)
button.titleLabel?.font = .systemFont(ofSize: 15.0)
button.layer.cornerRadius = 8
button.layer.masksToBounds = true
return button
}()
let hangUpButton: UIButton = {
let button = UIButton(frame: .zero)
button.backgroundColor = UIColor(red: 52.0/255, green: 184.0/255, blue: 97.0/255, alpha: 1)
button.setTitle(Localize("TRTC-API-Example.AudioCalling.hangup"), for: .normal)
button.layer.cornerRadius = 8
button.layer.masksToBounds = true
button.titleLabel?.font = .systemFont(ofSize: 15.0)
return button
}()
let dashBoardLabel: UILabel = {
let label = UILabel(frame: .zero)
label.text = Localize("TRTC-API-Example.AudioCalling.dashBorad")
label.textColor = UIColor(red: 52.0/255, green: 184.0/255, blue: 97.0/255, alpha: 1)
label.adjustsFontSizeToFitWidth = true
return label
}()
lazy var userBoxArray: [UserBox] = {
return [userBox1, userBox2, userBox3, userBox4, userBox5, userBox6]
}()
let trtcCloud: TRTCCloud = {
return TRTCCloud.sharedInstance()
}()
lazy var remoteInfoDictionary: [String: CustomRemoteInfo] = {
var dic = [String: CustomRemoteInfo]()
return dic
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
trtcCloud.delegate = self
constructViewHierarchy()
activateConstraints()
bindInteraction()
setupTRTCCloud()
navigationItem.title = navgationTitle + "\(roomId)"
navigationItem.leftBarButtonItem = nil
}
@objc func onSwitchSpeakerClick(sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
trtcCloud.getDeviceManager().setAudioRoute(.speakerphone)
} else {
trtcCloud.getDeviceManager().setAudioRoute(.earpiece)
}
}
@objc func onMicCaptureClick(sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
trtcCloud.muteLocalAudio(true)
} else {
trtcCloud.muteLocalAudio(false)
}
}
@objc func onAudioCallStopClick(sender: UIButton) {
trtcCloud.stopLocalAudio()
navigationController?.popViewController(animated: true)
}
func setupTRTCCloud() {
let params = TRTCParams()
params.sdkAppId = UInt32(SDKAPPID)
params.roomId = roomId
params.userId = userId as String
params.role = .anchor
params.userSig = GenerateTestUserSig.genTestUserSig(identifier: params.userId) as String
self.trtcCloud.enterRoom(params, appScene: .videoCall)
self.trtcCloud.startLocalAudio(.music)
self.trtcCloud.enableAudioVolumeEvaluation(1000)
}
deinit {
trtcCloud.exitRoom()
TRTCCloud.destroySharedIntance()
}
}
// MARK: - UI Layout
extension AudioCallingViewController {
//Build view
private func constructViewHierarchy() {
view.addSubview(userStatusTableView)
view.addSubview(hansFreeButton)
view.addSubview(hangUpButton)
view.addSubview(muteButton)
view.addSubview(stackView2)
view.addSubview(stackView1)
for i in 0..<userBoxArray.count {
if i/3 == 0 {
stackView1.addArrangedSubview(userBoxArray[i])
}
else if i/3 == 1 {
stackView2.addArrangedSubview(userBoxArray[i])
}
}
}
//view layout
private func activateConstraints() {
userStatusTableView.snp.makeConstraints { make in
make.width.equalTo(300)
make.height.equalTo(200)
make.top.equalTo(400)
make.leading.equalTo(45)
}
hansFreeButton.snp.makeConstraints { make in
make.bottom.equalTo(-40)
make.leading.equalTo(20)
make.height.equalTo(30)
make.width.equalTo(100)
}
hangUpButton.snp.makeConstraints { make in
make.bottom.equalTo(-40)
make.leading.equalTo(muteButton.snp.trailing).offset(15)
make.width.equalTo(100)
make.height.equalTo(30)
}
muteButton.snp.makeConstraints { make in
make.bottom.equalTo(-40)
make.leading.equalTo(hansFreeButton.snp.trailing).offset(15)
make.height.equalTo(30)
make.width.equalTo(100)
}
stackView1.snp.makeConstraints { make in
make.height.equalTo(90)
make.width.equalTo(300)
make.top.equalTo(100)
make.centerX.equalToSuperview()
}
stackView2.snp.makeConstraints { make in
make.height.equalTo(90)
make.width.equalTo(300)
make.top.equalTo(195)
make.centerX.equalToSuperview()
}
}
//Binding events
private func bindInteraction() {
userStatusTableView.delegate = self
userStatusTableView.dataSource = self
userStatusTableView.allowsSelection = false
hansFreeButton.addTarget(self, action: #selector(onSwitchSpeakerClick(sender: )), for: .touchUpInside)
muteButton.addTarget(self, action: #selector(onMicCaptureClick(sender: )), for: .touchUpInside)
hangUpButton.addTarget(self, action: #selector(onAudioCallStopClick(sender: )), for: .touchUpInside)
}
}
// MARK: - UITableViewDataSource
extension AudioCallingViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userIdArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: nil)
cell.textLabel?.textColor = UIColor(red: 52.0/255, green: 184.0/255, blue: 97.0/255, alpha: 1)
cell.backgroundColor = UIColor.clear
let userId = userIdArray[indexPath.row]
if indexPath.section == 1 {
guard let remoteInfo = remoteInfoDictionary[userId] else {
return UITableViewCell()
}
guard let volume = remoteInfo.volume else {
return UITableViewCell()
}
cell.textLabel?.text = userId + ":" + String(volume)
}
else if indexPath.section == 2 {
var quality: String = ""
switch remoteInfoDictionary[userId]?.quality {
case .excellent:
quality = "Best"
break
case .good:
quality = "Good"
break
default:
quality = "unknow"
}
cell.textLabel?.text = userId + ":" + quality
}
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0{
return dashBoardLabel.text
}
else if section == 1{
return Localize("TRTC-API-Example.AudioCalling.volumInfo")
}
else{
return Localize("TRTC-API-Example.AudioCalling.network")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0{
return dashBoardLabel
}
let header: UITableViewHeaderFooterView = UITableViewHeaderFooterView()
header.textLabel?.textColor = UIColor(red: 52.0/255, green: 184.0/255, blue: 97.0/255, alpha: 1)
return header
}
}
// MARK: - UITableViewDelegate
extension AudioCallingViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 15
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
}
//MARK: - TRTCCloudDelegate
extension AudioCallingViewController: TRTCCloudDelegate {
func onRemoteUserEnterRoom(_ userId: String) {
userIdArray.append(userId)
remoteInfoDictionary[userId] = CustomRemoteInfo()
refreshRemoteAudioViews()
}
func onRemoteUserLeaveRoom(_ userId: String, reason: Int) {
guard let index = userIdArray.firstIndex(of: userId) else {
return
}
userBoxArray[index].isHidden = true
userIdArray.remove(at: index)
remoteInfoDictionary.removeValue(forKey: userId)
refreshRemoteAudioViews()
}
func refreshRemoteAudioViews() {
for box in userBoxArray{
box.isHidden = true
}
var index = 0
for id in userIdArray {
if index >= maxRemoteUserNum {
return
}
userBoxArray[index].isHidden = false
userBoxArray[index].label.text = id
index += 1
}
}
func onNetworkQuality(_ localQuality: TRTCQualityInfo, remoteQuality: [TRTCQualityInfo]) {
for userId in userIdArray {
for info in remoteQuality {
if userId == info.userId {
remoteInfoDictionary[userId]?.quality = info.quality
}
}
}
userStatusTableView.reloadData()
}
func onUserVoiceVolume(_ userVolumes: [TRTCVolumeInfo], totalVolume: Int) {
for userId in userIdArray {
for info in userVolumes {
if userId != info.userId {
remoteInfoDictionary[userId]?.volume = info.volume
}
}
}
userStatusTableView.reloadData()
}
}