Latest Legacy

Initiate an audio stream

In this example, upon executing the audio Stream element, the audio tracks flowing in (inbound) and out (outbound) are simultaneously forked and streamed to the provided WebSocket service URL. The streaming duration is defined as 120 seconds, implying that the streaming operation will cease automatically once 120 seconds have elapsed.

Response

<Response>
	<Stream bidirectional="false" streamTimeout="120" audioTrack="both" statusCallbackUrl="https://<yourdomain>.com/events/" statusCallbackMethod="POST" contentType="raw">wss://yourstream.websocket.io/audiostream</Stream>
</Response>

Example Request

1
2
3
4
5
from plivo import plivoxml

response = plivoxml.ResponseElement().add(plivoxml.StreamElement('wss://yourstream.websocket.io/audiostream', bidirectional= "false",audioTrack="both",streamTimeout=120,statusCallbackMethod="POST",statusCallbackUrl="https://yourdomain.com/events/",contentType="audio/x-l16;rate=8000",extraHeaders="Test1=Test2,Test3=Test4"))

print(response.to_string())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
require 'rubygems'
require 'plivo'

include Plivo::XML
include Plivo::Exceptions

begin
  response = Response.new
    response.addStream('
wss://yourstream.websocket.io/audiostream', {
        bidirectional: "false", audioTrack: 'both',
        contentType: "audio/x-l16;rate=8000",
        streamTimeout: 120,
        statusCallbackUrl: "https://<yourdomain>.com/events/",
        statusCallbackMethod: "POST",
        extraHeaders:  "Test1=Test2,Test3=Test4"
    })

  xml = PlivoXML.new(response)
  puts xml.to_xml
rescue PlivoXMLError => e
  puts 'Exception: ' + e.message
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var plivo = require('plivo');

const streamResponse = new Response();
var service_url = "wss://yourstream.websocket.io/audiostream";
var extraHeaders = "{'Test':'test1'}";
var params = {bidirectional: "false", audioTrack: 'both',
        contentType: "audio/x-l16;rate=8000",
        streamTimeout: 120,
        statusCallbackUrl: "https://<yourdomain>.com/events/",
        statusCallbackMethod: "POST",
        extraHeaders:  "Test1=Test2,Test3=Test4"
};
streamResponse.addStream(service_url, params);

console.log(response.toXML());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
    require './vendor/autoload.php';
    use Plivo\XML\Response;

    $response = new Response();


    $params1 = array(
        'bidirectional' => false,
        'audio_track' => "both",
        'stream_timeout' => 120,
        'content_type' => 'audio/x-l16;rate=8000',
        'status_callback_url' => 'https://<yourdomain>.com/events/',
        'status_callback_method' => 'POST',
        'extraHeaders'=>'Test1=Test2,Test3=Test4'
    );

    $response->addStream("
wss://yourstream.websocket.io/audiostream",$params1);
    $ssml = $response->toXML(true);

    Header('Content-type: text/xml');
    echo($response->toXML());
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example for Audio Stream XML creation
package com.plivo.examples;

import com.plivo.api.exceptions.PlivoValidationException;
import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.xml.Response;
import com.plivo.api.xml.Stream;

public class StreamXML {

  public static void main(String[] args) throws PlivoValidationException, PlivoXmlException {
    Response response = new Response().children(new Stream("
wss://yourstream.websocket.io/audiostream
")
      .bidirectional(false)
      .streamTimeout(120)
      .audioTrack("both")
      .contentType("audio/x-l16;rate=8000")
      .statusCallbackUrl("callback-url")
      .statusCallbackMethod("POST")
      .extraHeaders("Test1=Test2,Test3=Test4"));
    System.out.println(response.toXmlString());
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;

namespace PlivoExamples
{
    public class MainClass
    {
        public static void Main(string[] args)
        {
            
            Dictionary<string, string> parameter  =
                new Dictionary<string, string>();
            parameter.Add("bidirectional", "False");
            parameter.Add("audioTrack", "both");
            parameter.Add("streamTimeout", "120");
            parameter.Add("statusCallbackUrl", "https://<yourdomain>.com/confevents/");
            parameter.Add("statusCallbackMethod", "POST");
            parameter.Add("contentType", "audio/x-l16;rate=16000");
            parameter.Add("extraHeaders", "Test1=Test2,Test3=Test4");
            
 Plivo.XML.Stream resp1 = new Plivo.XML.Stream("
wss://yourstream.websocket.io/audiostream",parameter);

            var output = resp1.ToString();
            Console.WriteLine(output);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
	"github.com/plivo/plivo-go/v7/xml"
	"html"
)

func main() {
	response := xml.ResponseElement{
		Contents: []interface{}{
			new(xml.StreamElement).SetAudioTrack("both").SetContentType("audio/x-l16;rate=16000").SetBidirectional(false).SetStatusCallbackMethod("POST").SetStatusCallbackUrl("https://yourdomain.com/events").SetStreamTimeout(120).SetExtraHeaders("Test1=Test2,Test3=Test4").SetContents("wss://www.example.com/socketserver"),
		},
	}
	print(html.UnescapeString(response.String()))
}