Let's Chat?

Enabling Voice

Prerequisites

Before you use Glance Voice, verify that you have added the Glance Mobile SDK for Android to your application (see ScreenshareDemo_kotlin for installation information). The Glance Mobile SDK includes Glance Voice functionality. You must also add the Twilio SDK to your project, as detailed below.

Enable Glance Voice

  1. Add the Twilio Programmable Voice SDK (v5.4.0) to your project gradle file, along with the following network dependencies that are required:
implementation 'com.twilio:voice-android:5.4.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
  1. Enable microphone permissions in your application. For more information, see Twilio documentation.
  2. Add this line to your Android Manifest file:<uses-permission android:name="android.permission.RECORD_AUDIO"/>
  3. Request microphone permissions within your application: String[] permissions = {Manifest.permission.RECORD_AUDIO};requestPermissions(permissions, 1);

Integration

There are two ways you can integrate Glance Voice into an app: the Default UI or a custom implementation.

  • The Glance Default UI provides a Glance Voice implementation that will work out-of-the-box, in conjunction with Glance Screenshare for iOS and Android.
  • The custom implementation provides the ability to completely control the voice and interface experience. Default UI with Glance Voice

Default Configuration

Glance Voice requires a Twilio JWT authentication token that can be retrieved securely from your server. An example of how to do this can be found in this project. You must call the following to configure the default UI before a session is started with GlanceVisitor.startSession.

Visitor.defaultUI(true, TERMS_URL, this, false);
Visitor.defaultUI(true, TERMS_URL, this, false);

See Visitor class for more informaiton about the arguments to this method.

Then, add DefaultUI.DefaultUIListener to your class, and implement the following two required methods:

void glanceDefaultUIVoiceAuthenticationRequired();
void glanceDefaultUIVoiceAuthenticationFailed();
void glanceDefaultUIVoiceAuthenticationRequired();
void glanceDefaultUIVoiceAuthenticationFailed();

The listener has many more methods, but these are the two that are required for voice authentication to work properly.

glanceDefaultUIVoiceAuthenticationRequired will get called after the user starts a session via the DefaultUI dialog. In this method, you can make a call to your server to retrieve the authentication token. Once you've done that, make a call to Visitor.defaultUIVoiceStartCall with the auth token:

Visitor.defaultUIVoiceStartCall(YOUR_AUTH_TOKEN);

That's it! You're all set.

OPTIONAL: For testing and development purposes, you can provide a GROUP_ID and API_KEY directly to the defaultUI API.

Visitor.defaultUI(true, TERMS_URL, GROUP_ID, API_KEY, null, false);
Visitor.defaultUI(true, TERMS_URL, GROUP_ID, API_KEY, null, false);

Custom Configuration

Include the Glance Voice header file.

import net.glance.glancevoicesdk.GlanceVoice
import net.glance.glancevoicesdk.GlanceVoice;

Availability

Glance Voice requires the Twilio SDK to work. Use the following methods to quickly check if Glance Voice is ready to be used and available.

An instance of the Context object must be passed to the isAvailable function.

if (GlanceVoice.isAvailable(context)){
    // Twilio SDK present and GlanceVoice is available
}
if (GlanceVoice.isAvailable(context)){
    // Twilio SDK present and GlanceVoice is available
}

Events

If you’re doing a custom implementation, you must implement a delegate or listener to react events. Here are the protocols or interfaces for the delegates and listeners and how to set them up on each platform.

public interface GlanceVoice.VoiceListener {
    /**
    * Called when {@link net.glance.glancevoicesdk.GlanceVoice#authenticate(String, String)} is successful.
    *
    * @param value The json encoded string with the params needed to make the call.
    */
    void authenticationSucceeded(String value);

    /**
    * Called when {@link net.glance.glancevoicesdk.GlanceVoice#authenticate(String, String)} fails
    * @param t A throwable that represents the cause of the failure.
    */
    void authenticationFailed(Throwable t);

    /**
    * Callback for when {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String)} or {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String, String, String, String)} is successful.
    */
    void startSucceeded();

    /**
    * Called when {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String)} or {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String, String, String, String)} fails
    * @param t A throwable that represents the cause of the failure.
    */
    void startFailed(Throwable t);

    /**
    * Called when the call terminates
    */
    void ended(Throwable t);

    /**
    * Called when a dropped call is attempting to reconnect
    */
    void reconnecting();

    /**
    * Called when a dropped call successfully reconnects
    */
    void reconnected();

    /**
    * Called when a dropped call times out attempting reconnect
    */
    void reconnectTimedOut();
}
GlanceVoice.setListener(listener)
public interface GlanceVoice.VoiceListener {
    /**
    * Called when {@link net.glance.glancevoicesdk.GlanceVoice#authenticate(String, String)} is successful.
    *
    * @param value The json encoded string with the params needed to make the call.
    */
    void authenticationSucceeded(String value);

    /**
    * Called when {@link net.glance.glancevoicesdk.GlanceVoice#authenticate(String, String)} fails
    * @param t A throwable that represents the cause of the failure.
    */
    void authenticationFailed(Throwable t);

    /**
    * Callback for when {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String)} or {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String, String, String, String)} is successful.
    */
    void startSucceeded();

    /**
    * Called when {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String)} or {@link net.glance.glancevoicesdk.GlanceVoice#startCall(Context, String, String, String, String)} fails
    * @param t A throwable that represents the cause of the failure.
    */
    void startFailed(Throwable t);

    /**
    * Called when the call terminates
    */
    void ended(Throwable t);

    /**
    * Called when a dropped call is attempting to reconnect
    */
    void reconnecting();

    /**
    * Called when a dropped call successfully reconnects
    */
    void reconnected();

    /**
    * Called when a dropped call times out attempting reconnect
    */
    void reconnectTimedOut();
}
GlanceVoice.setListener(listener)

Authenticate

With the delegate or listener in place, now you can make the first call to Glance Voice to authenticate with the Glance Voice group ID and API key.

GlanceVoice.authenticate("1234", "yourApiKey")
GlanceVoice.authenticate("1234", "yourApiKey");

Starting a Voice Session

The call to authenticate results in your delegate or listener invoking when the call succeeds or fails. If authentication succeeds, the JSON string passed to the delegate or listener method is required when starting a session. Verify that startCall does in fact succeed or manage its failure with the methods within the delegate or listener.

override fun authenticationSucceeded(authenticationValue : String){
    try {
        // mContext is a Context (an Activity or the ApplicationContext)
        GlanceVoice.startCall(mContext, authenticationValue);
    } catch (SecurityException e) {
        // Handle security exception that might occur
    }
}
public void authenticationSucceeded(String authenticationValue) {
    try {
        // mContext is a Context (an Activity or the ApplicationContext)
        GlanceVoice.startCall(mContext, authenticationValue);
    } catch (SecurityException e) {
        // Handle security exception that might occur
    }
}

Ending a VoiceSession

To end a voice session, invoke endCall.

GlanceVoice.endCall()
GlanceVoice.endCall();

Mute or Unmute a Session

Set the mute state with a boolean yes/no or true/false.

GlanceVoice.mute(true)
GlanceVoice.mute(true);

Enable or Disable Speakerphone

Toggle whether or not the audio of the call is played through the speaker of the phone.

GlanceVoice.speakerphone(true)
GlanceVoice.speakerphone(true);

By continuing to use the site, you agree to the use of cookies. Learn More