Class: Media

Media


new Media()

WebRTC Media class

Properties:
Name Type Description
application Application

The parent application object

parentConversation Conversation

the conversation object this media instance belongs to

Properties
Name Type Description
streamIndex number

the latest index of the streams, updated in each new peer offer

rtcObjects Array.<object>

data related to the rtc connection

Properties
Name Type Argument Description
rtc_id string

the rtc_id

pc PeerConnection

the current PeerConnection object

stream Stream

the stream of the specific rtc_id

type string <optional>

audio the type of the stream

streamIndex number

the index number of the stream (e.g. use to mute)

Source:
Fires:

Methods


_cleanMediaProperties()

Cleans up the user's media before leaving the conversation

Source:

disable()

Disable media participation in the conversation for this application
if RtcStats MOS is enabled, a final report will be available in
NexmoClient#rtcstats:report

Source:
Returns:
Type
Promise
Example
function disable() {
  conversation.media.disable()
     .then((response) => {
      }).catch((error) => {
          console.log(error);
      });
}

<async> enable(params)

Enable media participation in the conversation for this application (requires WebRTC)

Parameters:
Name Type Description
params object

rtc params

Properties
Name Type Argument Default Description
label string

Label is an application defined tag, eg. ‘fullscreen’

audio object <optional>
true

audio enablement mode. possible values "both", "send_only", "receive_only", "none", true or false

autoPlayAudio object <optional>
false

attach the audio stream automatically to start playing after enable media (default false)

Source:
Returns:
Type
Promise.<MediaStream>
Example

Enable media in this conversation

function enable() {
  conversation.media.enable()
     .then((stream) => {
         const media = document.createElement("audio");
         const source = document.createElement("source");
         const media_div = document.createElement("div");
         media.appendChild(source);
         media_div.appendChild(media);
         document.insertBefore(media_div);
         // Older browsers may not have srcObject
         if ("srcObject" in media) {
           media.srcObject = stream;
         } else {
           // Avoid using this in new browsers, as it is going away.
           media.src = window.URL.createObjectURL(stream);
         }
         media.onloadedmetadata = (e) => {
           media.play();
         };

		 }).catch((error) => {
          console.log(error);
     });
}

mute( [mute] [, streamIndex])

Mute our member

Parameters:
Name Type Argument Default Description
mute boolean <optional>
false

true for mute, false for unmute

streamIndex number <optional>
null

stream id to set - if it's not set all streams will be muted

Source:
Example

Mute audio stream in conversation

media.mute(true)

<async> playStream()

Play an audio stream in a conversation

Source:
Returns:
Type
Promise.<NXMEvent>

<async> sayText(params)

Play a voice text in a conversation

Parameters:
Name Type Description
params object
Properties
Name Type Description
text string

the text to say in the conversation

voice_name string
level number

[0] -

queue boolean
loop boolean
Source:
Returns:
Type
Promise.<NXMEvent>
Example
conversation.media.sayText({text:'hi'});

<async> sendDTMF(digit)

Send DTMF in a conversation

Parameters:
Name Type Description
digit string

the DTMF digit(s) to send

Source:
Returns:
Type
Promise.<NXMEvent>
Example
conversation.media.sendDTMF('digit');

<async> startRinging()

Send start ringing event

Source:
Returns:
Type
Promise.<NXMEvent>
Example
Send ringing event
function startRinging() {
  conversation.media.startRinging()
     .then((response) => {
      }).catch((error) => {
          console.log(error);
      });
}

conversation.on('audio:ringing:start', (data) => {
console.log("ringing");
});

<async> stopRinging()

Send stop ringing event

Source:
Returns:
Type
Promise.<NXMEvent>
Example
Send ringing event
function stopRinging() {
  conversation.media.stopRinging()
   .then((response) => {
   }).catch((error) => {
     console.log(error);
   });
}

conversation.on('audio:ringing:stop', (data) => {
 console.log("ringing stopped");
}

updateAudioConstraints(constraints, type)

Replaces the stream's audio tracks currently being used as the sender's sources with a new one

Parameters:
Name Type Argument Description
constraints object

audio constraints

type string

rtc object type

constraints.audio object <optional>

set audio constraints - { deviceId: { exact: microphoneId } }

Source:
Returns:
  • Returns the new stream.
Type
Promise.<MediaStream>
Example
<caption>Update the stream currently being used with a new one</caption>