Skip to content

Commit

Permalink
[Android][Compose]perfect audio route.
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfd3 committed Jun 27, 2024
1 parent d6b06d1 commit 9a356cd
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ fun JoinChannelAudio() {
var localUid by rememberSaveable { mutableIntStateOf(0) }
var videoIdList by rememberSaveable { mutableStateOf(listOf<Int>()) }
val statsMap = remember { mutableStateMapOf(0 to AudioStatsInfo()) }
var audioRoute by rememberSaveable { mutableIntStateOf(Constants.AUDIO_ROUTE_SPEAKERPHONE) }

val rtcEngine = remember {
RtcEngine.create(RtcEngineConfig().apply {
mAreaCode = SettingPreferences.getArea()
mContext = context
mAppId = BuildConfig.AGORA_APP_ID
mEventHandler = object : IRtcEngineEventHandler() {

override fun onAudioRouteChanged(routing: Int) {
super.onAudioRouteChanged(routing)
audioRoute = routing
}

override fun onJoinChannelSuccess(channel: String?, uid: Int, elapsed: Int) {
super.onJoinChannelSuccess(channel, uid, elapsed)
isJoined = true
Expand Down Expand Up @@ -103,6 +110,7 @@ fun JoinChannelAudio() {
}
}).apply {
enableAudio()
setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION)
}
}
DisposableEffect(lifecycleOwner) {
Expand All @@ -120,10 +128,12 @@ fun JoinChannelAudio() {
// Permission is granted
Toast.makeText(context, "Permission Granted", Toast.LENGTH_LONG).show()
val mediaOptions = ChannelMediaOptions()
mediaOptions.channelProfile = Constants.CHANNEL_PROFILE_LIVE_BROADCASTING
mediaOptions.clientRoleType = Constants.CLIENT_ROLE_BROADCASTER
mediaOptions.publishCameraTrack = false
mediaOptions.publishMicrophoneTrack = true
mediaOptions.autoSubscribeAudio = true
mediaOptions.autoSubscribeVideo = false
TokenUtils.gen(channelName, 0) {
rtcEngine.joinChannel("", channelName, 0, mediaOptions)
rtcEngine.joinChannel(it, channelName, 0, mediaOptions)
}

} else {
Expand All @@ -136,6 +146,7 @@ fun JoinChannelAudio() {
rtcEngine = rtcEngine,
videoIdList = videoIdList,
statsMap = statsMap,
audioRoute = audioRoute,
channelName = channelName,
isJoined = isJoined,
onJoinClick = {
Expand Down Expand Up @@ -167,6 +178,7 @@ private fun JoinChannelAudioView(
statsMap: Map<Int, AudioStatsInfo> = mapOf(),
rtcEngine: RtcEngine? = null,
channelName: String = "",
audioRoute: Int = Constants.AUDIO_ROUTE_SPEAKERPHONE,
isJoined: Boolean = false,
onJoinClick: (String) -> Unit = {},
onLeaveClick: () -> Unit = {}
Expand Down Expand Up @@ -197,9 +209,14 @@ private fun JoinChannelAudioView(
"Earphone" to Constants.AUDIO_ROUTE_EARPIECE,
"Bluetooth" to Constants.AUDIO_ROUTE_BLUETOOTH_DEVICE_HFP
),
selected = 0
selectedValue = audioRoute
) { _, option ->
rtcEngine?.setRouteInCommunicationMode(option.second)
val ret = rtcEngine?.setRouteInCommunicationMode(option.second)
if (ret != Constants.ERR_OK) {
// no in communication mode
val isSpeakerPhone = option.second == Constants.AUDIO_ROUTE_SPEAKERPHONE
rtcEngine?.setEnableSpeakerphone(isSpeakerPhone)
}
}
}
item {
Expand Down Expand Up @@ -267,7 +284,7 @@ private fun JoinChannelAudioView(
"Cloud Gaming" to Constants.CHANNEL_PROFILE_CLOUD_GAMING,
"Communication 1v1" to Constants.CHANNEL_PROFILE_COMMUNICATION_1v1,
"Communication 1v1" to Constants.CHANNEL_PROFILE_COMMUNICATION_1v1,
)
),
) { _, option ->
rtcEngine?.setChannelProfile(option.second)
}
Expand Down

0 comments on commit 9a356cd

Please sign in to comment.