Play an audio stream into a call
This code snippet plays an audio stream into the specified call.
Example
Replace the following variables in the example code:
Key | Description |
---|---|
UUID |
The UUID of the call leg |
URL |
The URL of the audio file that will be streamed into an array. The URL must be in an array. |
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 play-audio-into-a-call.sh
:
curl -X PUT https://api.nexmo.com/v1/calls/$UUID/stream \
-H "Authorization: Bearer "$JWT\
-H "Content-Type: application/json"\
-d '{"stream_url": ["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/assets/welcome_to_nexmo.mp3"]}'
Run your code
Save this file to your machine and run it:
bash play-audio-into-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 stream-audio-into-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 stream-audio-into-call.js
:
const URL = 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3';
vonage.calls.stream.start(UUID, { stream_url: [URL], loop: 0 }, (err, res) => {
if(err) { console.error(err); }
else {
console.log(res);
}
});
function stop_stream (){
vonage.calls.stream.stop(UUID, (err, res) => {
if(err) { console.error(err); }
else {
console.log(res);
}
});
}
setTimeout(stop_stream, 5000); // delay 5 seconds
Run your code
Save this file to your machine and run it:
node stream-audio-into-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 StreamAudioToCall
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 StreamAudioToCall
class:
final String ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/long-tts.json";
CallEvent call = client.getVoiceClient().createCall(new Call(
TO_NUMBER,
VONAGE_NUMBER,
ANSWER_URL
));
Thread.sleep(20000);
final String UUID = call.getUuid();
final String URL = "https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3";
client.getVoiceClient().startStream(UUID, URL, 0);
Thread.sleep(5000);
client.getVoiceClient().stopStream(UUID);
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 StreamAudioToCall
:
gradle run -Pmain=com.vonage.quickstart.voice.StreamAudioToCall
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 PlayAudioStreamIntoCall.cs
and add the following code:
using Vonage;
using Vonage.Request;
using Vonage.Voice;
Add the following to PlayAudioStreamIntoCall.cs
:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);
Write the code
Add the following to PlayAudioStreamIntoCall.cs
:
var command = new StreamCommand() { StreamUrl = new[] { URL } };
var response = client.VoiceClient.StartStream(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()->streamAudio(UUID, 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3');
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 play-audio-stream-into-call.py
and add the following code:
client = vonage.Client(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH
)
Write the code
Add the following to play-audio-stream-into-call.py
:
voice = vonage.Voice(client)
stream_url = 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3'
voice.send_audio(VONAGE_CALL_UUID, stream_url=[stream_url])
Run your code
Save this file to your machine and run it:
python3 play-audio-stream-into-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 play-audio-stream-into-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 play-audio-stream-into-call.rb
:
AUDIO_URL = 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3'
response = client.voice.stream.start(UUID, stream_url: [AUDIO_URL])
Run your code
Save this file to your machine and run it:
ruby play-audio-stream-into-call.rb
Try it out
When you run the code an audio stream from the file specified is played into the call identified with the specified UUID.