-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattendclass.kt
137 lines (119 loc) · 4.14 KB
/
attendclass.kt
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
package com.muhammadahmad.i210790
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.os.SystemClock
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import io.agora.rtc2.IRtcEngineEventHandler
import io.agora.rtc2.RtcEngine
import io.agora.rtc2.RtcEngineConfig
class attendclass : AppCompatActivity() {
private val PERMISSION_ID = 12
private val app_id = "36466575ce8a45c8b66c3efe957d9afd"
private val channelName = "voicecall"
private val token = "007eJxTYMhjjZZVyC7m+XpmZul/mVjPxUt5Jj3Z2mC5xsfuW86ZjCgFBmMzEzMzU3PT5FSLRBPTZIskM7Nk49S0VEtT8xTLxLSUGZUCaQ2BjAyXj/1kYWSAQBCfk6EsPzM5NTkxJ4eBAQC1cyG0"
private val uid = 0
private var isJoined = false
private var agoraEngine: RtcEngine? = null
private var startTime: Long = 0
private val handler = Handler()
private val REQUESTED_PERMISSION = arrayOf(
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission.INTERNET,
android.Manifest.permission.ACCESS_NETWORK_STATE
// Add more permissions as needed
)
private fun checkSelfPermission(): Boolean {
return ContextCompat.checkSelfPermission(
this, REQUESTED_PERMISSION[0]
) == PackageManager.PERMISSION_GRANTED
}
private fun setupVoiceSdkEngine() {
try {
val config = RtcEngineConfig()
config.mContext = baseContext
config.mAppId = app_id
config.mEventHandler = mRtcEventHandler
agoraEngine = RtcEngine.create(config)
agoraEngine!!.enableAudio()
} catch (e: Exception) {
showToast("Failed to initialize voice call: ${e.message}")
finish()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_attendclass)
if (!checkSelfPermission()) {
ActivityCompat.requestPermissions(
this, REQUESTED_PERMISSION, PERMISSION_ID
)
}
setupVoiceSdkEngine()
joinCall()
// Start timer
startTime = SystemClock.elapsedRealtime()
startTimer()
val hangupButton = findViewById<TextView>(R.id.joinbuttonn)
hangupButton.setOnClickListener {
hangupCall()
}
}
private fun leaveCall() {
if (!isJoined) {
return
}
agoraEngine?.leaveChannel()
isJoined = false
}
override fun onDestroy() {
super.onDestroy()
stopTimer()
agoraEngine?.leaveChannel()
RtcEngine.destroy()
agoraEngine = null
}
private fun joinCall() {
if (checkSelfPermission()) {
agoraEngine!!.joinChannel(token, channelName, "", uid)
} else {
showToast("Permission denied for audio recording")
finish()
}
}
private val mRtcEventHandler: IRtcEngineEventHandler =
object : IRtcEngineEventHandler() {
override fun onJoinChannelSuccess(channel: String?, uid: Int, elapsed: Int) {
isJoined = true
showToast("Voice call connected")
}
}
private fun startTimer() {
handler.postDelayed(timerRunnable, 1000)
}
private fun stopTimer() {
handler.removeCallbacks(timerRunnable)
}
private val timerRunnable = object : Runnable {
override fun run() {
val elapsedTime = SystemClock.elapsedRealtime() - startTime
val seconds = (elapsedTime / 1000) % 60
val minutes = (elapsedTime / 1000) / 60
val timeString = String.format("%02d:%02d", minutes, seconds)
findViewById<TextView>(R.id.textView5).text = timeString
handler.postDelayed(this, 1000)
}
}
private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
private fun hangupCall() {
leaveCall()
finish()
}
}