Player

The Player module allows to send audio (loaded from specified URL) or speech (generated by TTS) to any object that can receive media Call, Conference, Recorder) by using Player.sendMediaTo function. Audio will be mixed automatically if sent from a few players to one stream.

Add the following line to your scenario code to use the module:

require(Modules.Player);

All functions, events and classes of the module can be used only after this line.

Example

 require(Modules.Player);

 let call,
 player;

 VoxEngine.addEventListener(AppEvents.CallAlerting, function(e) {
		call = e.call;
	  	call.answer();
		call.addEventListener(CallEvents.Connected, function(callevent) {
	      	// Play intro speech
	    	player = VoxEngine.createTTSPlayer("Hello, you have called Voximplant Player demo", {"language": VoiceList.Amazon.en_US_Joanna});
	      	player.sendMediaTo(call);
	      	// When playback finished
	      	player.addEventListener(PlayerEvents.PlaybackFinished, function(playerevent) {
	          // Play mp3 file
	          player = VoxEngine.createURLPlayer("http://cdn.voximplant.com/yodl.mp3", {"loop": true});
	          player.sendMediaTo(call);
	          setTimeout(VoxEngine.terminate, 10000); // end the session in 10 sec
	      	});
	  	});
	  	call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate);
	});
  • Events
  • Player
  • VoxEngine.createTTSPlayer

    Create new audio player with specified text; TTS is used to play this text. Media streams can later be attached using the Call.sendMediaTo method etc.
    If text length exceeds 1500 characters the PlayerEvents.PlaybackFinished event is triggered with error description. After the very first playing, a phrase will be cached; each createTTSPlayer instance stores the cache data up to 2 weeks. Note that cache addresses only the URL, without additional headers. The cached phrase is available for all applications and further sessions.

  • VoxEngine.createToneScriptPlayer

    Create new audio player with specified ToneScript sequence. Media streams can later be attached using the Call.sendMediaTo method etc.

  • VoxEngine.createURLPlayer

    Creates a new audio player with the specified audio file URL. After the very first playback, a file will be cached; each 'createURLPlayer' instance stores the cache data up to 2 weeks. Note that cache addresses only the URL, without additional headers. The cached file is available for all applications and further sessions. File downloading has a timeout of 12 seconds. Reaching this timeout causes the "Timeout was reached" error. Media streams can later be attached using the Call.sendMediaTo method etc. IMPORTANT: each call object can send media to any number of other calls (media units), but can receive only one audio stream. A new incoming stream always replaces the previous one.

functions

VoxEngine.createTTSPlayer

VoxEngine.createTTSPlayer(text: String, TTSPlayerOptions: TTSPlayerOptions): Player

Create new audio player with specified text; TTS is used to play this text. Media streams can later be attached using the Call.sendMediaTo method etc.
If text length exceeds 1500 characters the PlayerEvents.PlaybackFinished event is triggered with error description. After the very first playing, a phrase will be cached; each createTTSPlayer instance stores the cache data up to 2 weeks. Note that cache addresses only the URL, without additional headers. The cached phrase is available for all applications and further sessions.

Parameters

  • text:

    String

    Text to say

  • TTSPlayerOptions:

    TTSPlayerOptions

    Parameters for TTS: language, progressive playback, volume, rate, etc.

Returns

VoxEngine.createToneScriptPlayer

VoxEngine.createToneScriptPlayer(script: String, ToneScriptPlayerOptions: ToneScriptPlayerOptions): Player

Create new audio player with specified ToneScript sequence. Media streams can later be attached using the Call.sendMediaTo method etc.

Parameters

  • script:

    String

    ToneScript string

  • ToneScriptPlayerOptions:

    ToneScriptPlayerOptions

    Parameters for ToneScript: loop, progressivePlayback, etc.

Returns

Examples

 var player = VoxEngine.createToneScriptPlayer("425@-19;5(1/4/1)");

VoxEngine.createURLPlayer

VoxEngine.createURLPlayer(url: String, URLPlayerOptions: URLPlayerOptions): Player

Creates a new audio player with the specified audio file URL. After the very first playback, a file will be cached; each 'createURLPlayer' instance stores the cache data up to 2 weeks. Note that cache addresses only the URL, without additional headers. The cached file is available for all applications and further sessions. File downloading has a timeout of 12 seconds. Reaching this timeout causes the "Timeout was reached" error. Media streams can later be attached using the Call.sendMediaTo method etc. IMPORTANT: each call object can send media to any number of other calls (media units), but can receive only one audio stream. A new incoming stream always replaces the previous one.

Parameters

  • url:

    String

    Url of an audio file. Supported formats are: mp3, ogg & flac (mp3, speex, vorbis and flac codecs respectively). Maximum file size is 10 Mb.

  • URLPlayerOptions:

    URLPlayerOptions

    Optional parameters: progressive playback, loop, onPause, etc.

Returns

Examples

 let player = VoxEngine.createURLPlayer("https://direct/link/to/the/file.mp3", {"loop": true});