Play an audio stream into a call

This building block 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 into which to play an audio stream.
URL The URL of the audio file that will be streamed into an array. The URL must be in an array.

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Write the code

Add the following to play-audio-into-a-call.sh:

Copy to Clipboard
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"]}'

View full source 

Run your code

Save this file to your machine and run it:

$ bash play-audio-into-a-call.sh

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ npm install nexmo dotenv
Initialize your dependencies

Create a file named app.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo');

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_PRIVATE_KEY
}, {debug: true});

View full source 

Write the code

Add the following to app.js:

Copy to Clipboard
const AUDIO_URL = 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3';

nexmo.calls.stream.start(CALL_UUID, { stream_url: [AUDIO_URL], loop: 0 }, (err, res) => {
  if(err) { console.error(err); }
  else {
      console.log(res);
  }
});

function stop_stream (){
    nexmo.calls.stream.stop(CALL_UUID, (err, res) => {
        if(err) { console.error(err); }
        else {
            console.log(res);
        }
    });
}

setTimeout(stop_stream, 5000); // delay 5 seconds

View full source 

Run your code

Save this file to your machine and run it:

$ node app.js

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

Add the following to build.gradle:

compile 'com.nexmo:client:latest.release'
Initialize your dependencies

Create a file named StreamAudioToCall.java and add the following code:

Copy to Clipboard
AuthMethod auth = new JWTAuthMethod(NEXMO_APPLICATION_ID, FileSystems.getDefault().getPath(NEXMO_PRIVATE_KEY));
NexmoClient nexmo = new NexmoClient(auth);

View full source 

Write the code

Add the following to StreamAudioToCall.java:

Copy to Clipboard
nexmo.getVoiceClient().startStream(UUID, URL, 0);

View full source 

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.nexmo.quickstart.voice with the package containing StreamAudioToCall:

gradle run -Pmain=com.nexmo.quickstart.voice.StreamAudioToCall

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ Install-Package Nexmo.Csharp.Client
Initialize your dependencies

Create a file named VoiceController.cs and add the following code:

Copy to Clipboard
var client = new Client(creds: new Nexmo.Api.Request.Credentials
{
    ApiKey = "NEXMO_API_KEY",
    ApiSecret = "NEXMO_API_SECRET",
    ApplicationId = "NEXMO_APPLICATION_ID",
    ApplicationKey = "NEXMO_APPLICATION_PRIVATE_KEY"
});

View full source 

Write the code

Add the following to VoiceController.cs:

Copy to Clipboard
var result = Client.Call.BeginStream(NEXMO_CALL_UUID, new Call.StreamCommand
{
    stream_url = new[]
    {
        "https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3"
    }
});

View full source 

Run your code

Save this file to your machine and run it:

$ Run using your IDE

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ composer require nexmo/client
Initialize your dependencies

Create a file named index.php and add the following code:

Copy to Clipboard
$keypair = new \Nexmo\Client\Credentials\Keypair(
    file_get_contents(NEXMO_APPLICATION_PRIVATE_KEY_PATH),
    NEXMO_APPLICATION_ID
);

$client = new \Nexmo\Client($keypair);

View full source 

Write the code

Add the following to index.php:

Copy to Clipboard
$stream = $client->calls[UUID]->stream();
$stream->setUrl('https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3');
$stream->put();

View full source 

Run your code

Save this file to your machine and run it:

$ php index.php

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ pip install nexmo
Initialize your dependencies

Create a file named play-audio-stream-into-call.py and add the following code:

Copy to Clipboard
import nexmo

client = nexmo.Client(
    application_id=APPLICATION_ID,
    private_key=APPLICATION_PRIVATE_KEY_PATH
)

View full source 

Write the code

Add the following to play-audio-stream-into-call.py:

Copy to Clipboard
stream_url = 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3'
client.send_audio(NEXMO_CALL_UUID, stream_url=stream_url)

View full source 

Run your code

Save this file to your machine and run it:

$ python3 play-audio-stream-into-call.py

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

$ gem install nexmo
Initialize your dependencies

Create a file named play-audio-stream-into-call.rb and add the following code:

Copy to Clipboard
require 'nexmo'

client = Nexmo::Client.new(
    application_id: NEXMO_APPLICATION_ID,
    private_key: File.read(NEXMO_APPLICATION_PRIVATE_KEY_PATH)
)

View full source 

Write the code

Add the following to play-audio-stream-into-call.rb:

Copy to Clipboard
AUDIO_URL = 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3'
response = client.calls.stream.start(UUID, stream_url: [AUDIO_URL])

puts response.inspect

View full source 

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.