Volume control and mute
To use Voice SDK for audio communication, you implement a simple workflow in your game. The game joins a new or an existing channel using an app ID and an authentication token. If a channel of the given name exists within the context of the app ID, the game joins the channel. If the named channel does not exist, a new channel is created that other users may join. Once the game joins a channel, it subscribes to one or more of the audio streams published in the channel. The game also publishes its own audio stream that other users in the channel subscribe to. Each user publishes streams that share their microphone or audio data.
The basics of joining and leaving a channel are presented in the SDK quickstart for Voice Calling. This document explains the common workflows you need to handle in your app.
Understand the tech
The following figure shows the typical workflow you integrate into your game in order to publish and subscribe to audio streams.
This page shows you how to add the following features to your game:
-
Volume control
Users in a channel often need to adjust the volume for local and remote audio. For example, they may want to increase the input volume from remote users and decrease the local audio recording volume. You use Voice SDK to enable game users to adjust recording and playback volumes. Voice SDK also provides methods to mute local audio, audio from a particular remote user, or audio from all remote users. Voice SDK provides several methods to manage local and remote audio volumes. These methods enable the user to control:
- Recording signal volume: The volume of the signal recorded by the local user.
- Playback signal volume: The playback audio level, after audio mixing, for all remote users, or for a specific remote user.
- In-ear monitoring volume: The volume of their own voice, users hear through wired earphones.
- Audio mixing volume: The mixing volume for local playback and for sending to other users.
- Muting audio streams: Stop or resume playing a specified remote or local audio.
Prerequisites
To follow this procedure you must have implemented the SDK quickstart project for Voice Calling.
Project setup
To create the environment necessary to implement screen sharing and volume control features into your game, open the SDK quickstart Voice Calling project you created previously.
Implement volume control
This section shows how to use Voice SDK to implement volume control in your game, step-by-step.
Implement the user interface
In a real-world application, for each volume setting you want the user to control, you typically add a volume control UI element such as a Slider to the audio configuration panel. To enable the user to mute local or remote audio, you add a Toggle
to the interface for each user. In this example, you add a Toggle
and a Slider
to the UI to test different volume settings.
-
Right-click Sample Scene, then click Game Object > UI > Slider. A Slider appears in the Scene Canvas.
-
In Inspector, rename Slider to
mediaProgressBar
, and then change the following coordinates:- Pos X - 0
- Pos Y - 142
-
Right-click Canvas, then click UI > Toggle. A toggle button appears in the scene canvas.
-
In Inspector, rename Toggle to
Mute
, then change Pos Y to30
.
Handle the system logic
-
Add the required library
To import the required Unity UI library, in your script file, add the following to the namespace declarations:
Implement volume control and screen sharing logic
To implement these features in your game, take the following steps:
-
Declare the variables you need
To read, store and apply workflow settings, in your script file, add the following declaration to
NewBehaviourScript
: -
Access the UI elements
To access the slider and teh toggle button, in your script file, add the following at the end of
SetupUI
method toNewBehaviourScript
: -
Adjust the recorded audio volume
When you drag the slider left or right, your game calls the
changeVolume
method. The method reads the slider value and callsAdjustRecordingSignalVolume
to adjust the recording signal volume. To implement this logic, in your script file, add the following method toNewBehaviourScript
:When using a device to capture audio, Voice SDK sets a default global volume value of 85 (range [0, 255]). Voice SDK automatically increases a capture device volume that's too low. You can adjust the capture volume as per your needs by adjusting the microphone or sound card's signal capture volume.
-
Get the
uid
of the remote userWhen a remote user joins the channel, Voice SDK triggers
OnUserJoined
. You use this callback to save theuid
of remote user. To implement this callback, in your script file, add the following callback toUserEventHandler
: -
Implement the logic to mute and unmute the remote user
When the user taps the mute toggle button, your game calls
muteRemoteAudioStream
and mutes the remote user. To implement this workflow, in your script file, add the following toNewBehaviourScript
:
Test your implementation
To ensure that you have implemented Voice Calling in your game:
-
Generate a temporary token in Agora Console .
-
In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.
-
In your script file, update
_appID
,_channelName
and_token
with the values for your temporary token. -
In Unity Editor, click Play. A moment later you see the game running on your development device.
-
Press Join to connect to the same channel as your web demo.
-
Test volume control
a. Speak into your Android device as you move the slider to the left and then right. You notice the volume decrease and then increase in the web demo app as the recording volume changes.
b. Tap the Mute
Toggle
while you speak into the microphone connected to the web demo app. You notice that the remote audio is muted on your Android device.c. To test other volume control methods, in
changeVolume
replace theAdjustRecordingSignalVolume
call with one of the following:Run the app again and use the volume slider to test the change in the corresponding volume setting.
d. To test other mute methods, in
muteRemoteAudio
replace theMuteRemoteAudioStream
call with one of the following:Run the app again and tap the
CheckBox
to test the effect of these mute methods.
-
In Visual Studio, in
AgoraImplementation.h
, updateappId
,channelName
andtoken
with the values for your temporary token. -
In Visual Studio, click Local Windows Debugger. A moment later, you see the app running on your development device.
If this is the first time you run your app, grant camera and microphone permissions.
-
Press Join to connect to the same channel as your web demo.
-
Test volume control
-
Speak into your development device as you move the slider on the slider to the left and then right. You notice the volume decrease and then increase in the web demo app as the recording volume changes.
-
Tap Mute while you speak into the microphone connected to the web demo app. You notice that the remote audio is muted on your development device.
-
To test other volume control methods, in
AgoraImplementationDlg.cpp
, replace theadjustRecordingSignalVolume
call inOnNMCustomdrawSlider2
with one of the following:Run the app again and use the volume slider to test the change in the corresponding volume setting.
-
To test other mute methods, in
AgoraImplementationDlg.cpp
, replace themuteRemoteAudioStream
call inOnBnClickedCheck1
with one of the following:Run the app again and tap the
CheckBox
to test the effect of these mute methods.
-
Reference
This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.