Pages Menu
TwitterRssFacebook

Posted by on May 27, 2015 in Learn Skype Web SDK

Learn Skype Web SDK Day 20 : Starting a New Audio Conversation

Learn Skype Web SDK Day 20 : Starting a New Audio Conversation

This is one post in a series of more than 30, devoted to helping you get up to speed quickly with the new Skype Web SDK. Each lesson comes with source code (in GitHub) and a working demo page so you can see what it does. Check out the full list of posts on the Skype Web SDK page.


View Demo

Starting an audio call using Skype for Web API is very similar to starting an Instant Message conversation. The actual setup of the conversation and adding participants is identical – in fact the only real difference is that instead of starting the Chat Service, we start the Audio Service.

Conversations are started, stopped and managed from the conversationManager, which is a member of the top-level parent Application. Creating a new conversation with Skype for Web API is actually a lot easier than you might think. It consists of 4 distinct steps: create the conversation, add participants, register the new conversation, start the conversation.

In order to make audio calls, you need to make sure the Plugin is installed.

Step 1 – Create the conversation. Like the rest of Skype for Web API – new objects are not ever instantiated in isolation – they always come from a parent. In this case we use the parent conversationManager to create a new conversation:

var application;
//instantiate application object and do sign-in process
var conversation = application.conversationsManager.createConversation();

Step 2 – Add the participants. You don’t need to add yourself, but you need to add anyone else in the conversation. Assuming you already have the person object:

var conversationParticipant = conversation.createParticipant(person);
conversation.participants.add(conversationParticipant);

Step 3 – Register the conversation. Before you start the conversation you need to tell the conversationsManager about it by adding it into its list of conversation:

application.conversationsManager.conversations.add(conversation);

Step 4 – Start the conversation! You’re now ready to begin the Audio Service, which you do with:

conversation.audioService.start();

At this point, the remote participant will receive the incoming conversation. So that you can track whether they answer it, or ignore/reject it you need to know its state. You can get this by subscribing to state change events on the local participants audio state:

conversation.selfParticipant.audio.state.changed(function(newState) {
//do something with newState
});

Once you start the audioService the state will change to “Connecting”. When the remote participant accepts, the state will change to “Connected”. If they reject or ignore the call it will change to “Disconnected”.

If you find that the state goes staight to “Disconnected” immediately without contacting the remote participant, have a look at the Console Log. If there’s been an error you should find it there. For instance, your Log list might look like this:

AudioVideoModality(a5df8786)::state: Created -> Connecting Reason: undefined

PluginManager::init - id = __mainPluginManager_e51d94cc_cd85_4263_ab0f_2e7cad9c819b

PluginManager::onPluginObjectState None
PluginManager::init - creating inner object
PluginObject::createInnerObject __mainPluginManager_e51d94cc_cd85_4263_ab0f_2e7cad9c819b_0

PluginManager::onPluginObjectState NotInstalled
PluginManager::cleanupPluginObject

PluginObject::destroyInnerObject __mainPluginManager_e51d94cc_cd85_4263_ab0f_2e7cad9c819b_0
PluginObject::cleanup __mainPluginManager_e51d94cc_cd85_4263_ab0f_2e7cad9c819b_0
AudioVideoSession(cc2e6069-6bb8-46ea-be68-8fa83373942a) State: Connecting -> Disconnected

AudioVideoModality(a5df8786)::state: Connecting -> Disconnected Reason: undefined

Here it’s pretty easy to see that the Plugin isn’t installed.

Today’s example code shows how to start an audio conversation using Skype Web SDK:

 <div class="form-horizontal">
    <div class="form-group">
      <label for="username" class="col-sm-2 control-label">Username</label>
      <div class="col-sm-10">
        <input type="email" class="form-control" id="username" placeholder="Email">
      </div>
    </div>
    <div class="form-group">
      <label for="password" class="col-sm-2 control-label">Password</label>
      <div class="col-sm-10">
        <input type="password" class="form-control" id="password" placeholder="Password">
      </div>
    </div>

    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <button class="btn btn-default" id="btnLogIn">Log in</button>
      </div>
    </div>

    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <button class="btn btn-default" id="btnLogOut">Log out</button>
      </div>
    </div>
  </div>

  <div>
    <span id="loginStatus"></span>
  </div>

  <hr/>

  <div class="form-horizontal">
    <div class="form-group">
      <label for="contact" class="col-sm-2 control-label">Start Audio Call with:</label>
      <div class="col-sm-10">
        <input type="email" class="form-control" id="contact" placeholder="Contact SIP Address">
      </div>
    </div>  

    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <button class="btn btn-default" id="btnStartConversation" disabled="disabled">Start</button>
      </div>
    </div>
  </div>
  
  <div>
    <span id="audioStatus"></span>
  </div>
<div id="footer"></div>
  
  <!-- This is not needed for the samples to run, but adds standard headers and footers to the samples, to display title, instructions, about etc.
       If you're taking this code and using it yourself, you can remove this.-->
   <script type="text/javascript" src="../../assets/layoutcodesample-min.js"></script>
  
  <script type="text/javascript">

  <!-- These variables are only needed for laying out the code sample, they are not part of the sample code. -->
  var pageTitle = 'Starting an Audio Conversation';
  var blogPostLocation = "http://thoughtstuff.co.uk";
  var githubLocation = "http://github.com";
 


 var client;
  $(function () {
    'use strict';

	Skype.initialize({
            apiKey: 'SWX-BUILD-SDK',
        }, function (api) {
            client = new api.application();           
			
		// whenever client.state changes, display its value
		client.signInManager.state.changed(function (state) {
		$('#loginStatus').text("Login State: " + state);
		});
	
        }, function (err) {
            alert('Error loading Skype Web SDK: ' + err);
        }); 

    $('#btnLogIn').click(function () {

        // start signing in
        client.signInManager.signIn({
          username: $('#username').val(),
          password: $('#password').val()
        }).then(function () {
            //log in worked!
            alert('Logged in!');
            $('#btnStartConversation').prop('disabled', false);
            
          }, function (error) {
            //Something went wrong.
            alert(error);
          });
      });

    $('#btnLogOut').click(function () {
        // start signing out
        client.signInManager.signOut()
        .then(function () {
               //log out worked!
               alert('Logged out!');
               $('#btnStartConversation').prop('disabled', true);
             }, function (error) {
                //Something went wrong.
                alert(error);
              });
      });

    $('#btnStartConversation').click(function () {
      StartConversation($('#contact').val());
    });

  function StartConversation (contactSIP) {
  //first, get the person to start a conversation with. Assume one person.
  var person;
  GetContactFromName(contactSIP).then(function (results) {
    results.forEach(function (result) {
    person = result.result;          
  });

 //create the conversation object
 var conversation = client.conversationsManager.createConversation();

 //add the person to the conversation by creating a conversation participant object
 var convParticipant = conversation.createParticipant(person);
 conversation.participants.add(convParticipant);

 //add the newly created conversation to the ConversationManager list
 client.conversationsManager.conversations.add(conversation);

 
 conversation.audioService.start();
 
 
 //watch the audio state of the self-participant to see what's happening
 conversation.selfParticipant.audio.state.changed(function(newState) {
	$('#audioStatus').text("Audio State: " + newState);
 });
});

}

function GetContactFromName(contactSIP)
{
 var query = client.personsAndGroupsManager.createPersonSearchQuery();
 query.text(contactSIP);
 query.limit(1);
 return query.getMore();          
}

});

</script>

Demo Online

You can try this code out against your own Skype for Business environment by going to the demo page. From here you can also download the code from GitHub if you want to host it locally, or take it and use it in your next project.

Disclaimer: This is sample code, intended to inform and educate. It is not production-ready and is lacking key components such as error handling. You use it entirely at your own risk. You should fully understand the effects, limitations and risks of the code before executing, and understand the implications of any set-up steps. By using these code examples you are using the Skype Web SDK, so you should read the Skype Software License Terms to which you are agreeing.

Good to Know

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

2 Comments

  1. Hi Tom
    Can you please tell what is the Plugin it is looking for. We have Skype for business Client installed on our systems but still getting error message that Plugin not installed. Your help is highly appreciated.

  2. hey tom,

    i too am having the same issue with the console showing that the plugin is not installed… in any browser, even though i know for sure it is as i use it for testing joining meetings over the web.

    any ideas?

    thanks!

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.