Avaya Client SDK

< Back to Package Overview

Working with Media Devices

This articles discusses basic concepts and features relating to working with devices. Media devices can be added or removed from the environment at any time, imposing requirements that must be understood and managed by your application.

The following articles are recommended to get you started:

Retrieve the list of media devices

You can retrieve all available audio devices by using the Devices collection. First of all get the MediaServicesInstance object and access the AudioInterface.

MediaServicesInstance mediaServices = client.getMediaEngine();
AudioInterface audioInterface = mediaServices.getAudioInterface();

Iterate the collection returned by the getDevices() method of the AudioInterface object to get the list of audio devices. All types of audio devices (handset, speaker, etc) are listed in the AudioDevice.Type.

Log.d(LOG_TAG, "Audio devices:");
for (AudioDevice audioDevice: audioInterface.getDevices()) {
    Log.d(LOG_TAG, audioDevice.getType().toString() + " " + 
        audioDevice.getName());
}

To check whether video camera is available or not, instantiate the VideoCaptureController object and use the hasVideoCamera() method.

VideoCaptureController videoCaptureController = new VideoCaptureController();
try {
    boolean hasFrontCamera = 
        videoCaptureController.hasVideoCamera(VideoCamera.Front);
    boolean hasBackCamera = 
        videoCaptureController.hasVideoCamera(VideoCamera.Back);
    if (!hasFrontCamera && !hasBackCamera) {
        Log.d(LOG_TAG, "Device doesn't have cameras.");
    }
    else {
        if (hasFrontCamera) {
            Log.d(LOG_TAG, "Device has a front camera.");
        }
        if (hasBackCamera) {
            Log.d(LOG_TAG, "Device has a back camera.");
        }
    }
} catch (VideoCaptureException e) {
    Log.e(LOG_TAG, "The hasVideoCamera method threw exception: " +
        e.getLocalizedMessage());
}

Setting an audio device to be active

Use the setUserRequestedDevice() method of the AudioInterface object to set device to be active.

audioInterface.setUserRequestedDevice(selectedAudioDevice);

Setting a video camera to be active

Start video capture from selected camera to set it to be active. Call the useVideoCamera() method of the VideoCaptureController object.

VideoCamera selectedVideoCameraType = VideoCamera.Front; // or VideoCamera.Back
videoCaptureController.useVideoCamera(selectedVideoCameraType, 
    new VideoCaptureCompletionHandler() {
        @Override
        public void onSuccess() {
            Log.d(LOG_TAG, "Camera has been selected");
        }

        @Override
        public void onError(VideoCaptureException e) {
            Log.e(LOG_TAG, "Failed to use camera. " + e.getLocalizedMessage());
        }
    });

Listening for audio device change events

In order to get notifications for audio device list changes, your application can define an object that implements the AudioDeviceListener interface.

class AudioDeviceHandler implements AudioDeviceListener
{
    private java.util.List currentDeviceList;

    public AudioDeviceHandler() {
        currentDeviceList = new ArrayList();
    }

    @Override
    public void onAudioDeviceError(AudioDeviceError error) {
        Log.e(LOG_TAG, "Audio device error: " + error.toString());
    }

    @Override
    public void onAudioDeviceListChanged(
        java.util.List newDeviceList,
        boolean activeDeviceChanged) {

        java.util.List addedDevices =
            new ArrayList(newDeviceList);
        java.util.List removedDevices =
            new ArrayList(currentDeviceList);
        addedDevices.removeAll(currentDeviceList);
        removedDevices.removeAll(newDeviceList);

        currentDeviceList = newDeviceList;

        if (addedDevices.size() > 0) {
            Log.d(LOG_TAG, "Audio devices has been added:");
            for (AudioDevice audioDevice: addedDevices) {
                Log.d(LOG_TAG, audioDevice.getType().toString() + " " +
                    audioDevice.getName());
            }
        }
        if (removedDevices.size() > 0) {
            Log.d(LOG_TAG, "Audio devices has been removed:");
            for (AudioDevice audioDevice: removedDevices) {
                Log.d(LOG_TAG, audioDevice.getType().toString() + " " +
                    audioDevice.getName());
            }
        }
    }

}

Then you can instantiate an audio device handler (AudioDeviceHandler) and add it as a listener to the AudioInterface object.

AudioDeviceHandler audioDeviceHandler = new AudioDeviceHandler();
audioInterface.addAudioDeviceListener(audioDeviceHandler);