adding delegate methods and making speaker phone work

This commit is contained in:
talksik
2021-12-30 02:07:13 -08:00
parent 242ac60629
commit 6d6a5ebe85
2 changed files with 54 additions and 6 deletions
+46 -5
View File
@@ -9,13 +9,17 @@ import Foundation
import AgoraRtcKit
class ConvoViewModel: ObservableObject {
class ConvoViewModel: NSObject, ObservableObject {
@Published var inCall = false
let firestoreService = FirestoreService()
var agoraKit: AgoraRtcEngineKit?
var agoraDelegate: AgoraRtcEngineDelegate?
init() {
override init() {
super.init()
// initiate convo listener to get all relevant convos
// any convo that is active
// and any of my active friends are in that convo
@@ -58,19 +62,27 @@ class ConvoViewModel: ObservableObject {
// ensure that this user leaves all other channels
self.leaveConvo()
self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
// finally join channel
agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in
self.agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in
self.inCall = true
})
}
// anyone can leave at any time
private func leaveConvo() {
func leaveConvo() {
agoraKit?.leaveChannel(nil)
// if I am the second person in the room, then end the convo
self.inCall = false
}
// if you are the second to last person in the convo and you leave, you end the convo
private func endConvo() {
self.leaveConvo()
AgoraRtcEngineKit.destroy()
}
}
@@ -81,3 +93,32 @@ extension ConvoViewModel {
agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: agoraDelegate)
}
}
extension ConvoViewModel: AgoraRtcEngineDelegate {
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinChannel channel: String, withUid uid: UInt, elapsed: Int) {
print("I did join channel")
}
func rtcEngine(_ engine: AgoraRtcEngineKit, didLeaveChannelWith stats: AgoraChannelStats) {
print("I did leave channel")
}
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
// Only one remote video view is available for this
// tutorial. Here we check if there exists a surface
// view tagged as this uid.
print("new user joined \(uid)")
}
func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid: UInt, reason: AgoraUserOfflineReason) {
print("user joined left")
}
func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurWarning warningCode: AgoraWarningCode) {
print("did occur warning: \(warningCode.rawValue)")
}
func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) {
print("did occur error: \(errorCode.rawValue)")
}
}
+8 -1
View File
@@ -49,7 +49,14 @@ struct ConvosView: View {
.scaleEffect(self.animate ? 1 : 0.85)
.animation(animate ? Animation.easeInOut(duration: 4).repeatForever(autoreverses: true) : .default)
.onTapGesture {
self.vm.joinConvo(convoId: testConvos[index].id!, convoAgoraToken: testConvos[index].agoraToken)
// if not in a call already
if !self.vm.inCall {
self.vm.joinConvo(convoId: testConvos[index].id!, convoAgoraToken: testConvos[index].agoraToken)
return
}
self.vm.leaveConvo()
}
}
}