Mute a call
This code snippet mutes / unmutes a call.
Example
Replace the following variables in the example code:
Key | Description |
---|---|
UUID |
The UUID of the call leg |
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Execute the following command at your terminal prompt to create the JWT for authentication:
export JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$NEXMO_APPLICATION_ID)
Write the code
Add the following to mute-a-call.sh
:
curl -X PUT https://api.nexmo.com/v1/calls/$UUID \
-H "Authorization: Bearer "$JWT\
-H "Content-Type: application/json"\
-d '{"action": "mute"}'
sleep 5s
curl -X PUT https://api.nexmo.com/v1/calls/$UUID \
-H "Authorization: Bearer "$JWT\
-H "Content-Type: application/json"\
-d '{"action": "unmute"}'
Run your code
Save this file to your machine and run it:
bash mute-a-call.sh
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
npm install @vonage/server-sdk
Create a file named mute-a-call.js
and add the following code:
const Vonage = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY
}, {debug: true});
Write the code
Add the following to mute-a-call.js
:
vonage.calls.update(UUID, { action: 'mute' }, (err, res) => {
if(err) { console.error(err); }
else {
console.log(res);
}
});
function unmute (){
vonage.calls.update(UUID, { action: 'unmute' }, (err, res) => {
if(err) { console.error(err); }
else {
console.log(res);
}
});
}
setTimeout(unmute, 5000); // delay 5 seconds
Run your code
Save this file to your machine and run it:
node mute-a-call.js
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Add the following to `build.gradle`:
compile 'com.vonage:client:5.5.0'
Create a class named MuteCall
and add the following code to the main
method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();
Write the code
Add the following to the main
method of the MuteCall
class:
/*
Establish a call for testing purposes.
*/
CallEvent call = client.getVoiceClient().createCall(new Call(
TO_NUMBER,
VONAGE_NUMBER,
"https://nexmo-community.github.io/ncco-examples/long-tts.json"
));
/*
Give them time to answer.
*/
Thread.sleep(10000);
final String UUID = call.getUuid();
client.getVoiceClient().modifyCall(UUID, ModifyCallAction.MUTE);
Thread.sleep(3000);
client.getVoiceClient().modifyCall(UUID, ModifyCallAction.UNMUTE);
Run your code
We can use the application
plugin for Gradle to simplify the running of our application.
Update your build.gradle
with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''
Run the following gradle
command to execute your application, replacing com.vonage.quickstart.voice
with the package containing MuteCall
:
gradle run -Pmain=com.vonage.quickstart.voice.MuteCall
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Install-Package Vonage
Create a file named MuteCall.cs
and add the following code:
using Vonage;
using Vonage.Request;
using Vonage.Voice;
Add the following to MuteCall.cs
:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);
Write the code
Add the following to MuteCall.cs
:
var command = new CallEditCommand() { Action = CallEditCommand.ActionType.mute };
var response = client.VoiceClient.UpdateCall(UUID, command);
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
composer require vonage/client
Create a file named index.php
and add the following code:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);
Write the code
Add the following to index.php
:
$client->voice()->muteCall(UUID);
sleep(3);
$client->voice()->unmuteCall(UUID);
Run your code
Save this file to your machine and run it:
php index.php
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
pip install vonage
Create a file named mute-a-call.py
and add the following code:
voice = Voice(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH
)
Write the code
Add the following to mute-a-call.py
:
response = voice.update_call(UUID, action="mute")
pprint(response)
time.sleep(5)
response = voice.update_call(UUID, action="unmute")
pprint(response)
Run your code
Save this file to your machine and run it:
python3 mute-a-call.py
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
gem install vonage
Create a file named mute-a-call.rb
and add the following code:
client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
Write the code
Add the following to mute-a-call.rb
:
client.voice.mute(UUID)
sleep(5)
Run your code
Save this file to your machine and run it:
ruby mute-a-call.rb
Try it out
When you run the code the call is muted or unmuted depending on the action specified.