Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Example of Android application managing media devices

This example can be used as streamer allowing to select source camera and microphone and specify parameters for the published video: FPS (Frames Per Second) and resolution (width, height).

On the screenshot below the example is displayed when a stream is being published.
In the URL specified in the 'WCS URL' input field

  • is the address of the WCS server
  • testStream is the stream name

Below that input field are located drop-down lists of available microphones and cameras and input fields for FPS, width and height.
Two videos are played

  • left - video from the camera
  • right - the published video stream as received from the server

Work with code of the example

To analyze the code, let's take class of the media-devices example version with hash 4ed4c6d77, which can be downloaded with corresponding build

1. Initialization of the API. line 76


For initialization, object Сontext is passed to the init() method.

2. List available media devices.

After initialization of the API, method getMediaDevices(), which returns MediaDeviceList object, is used to request list of all available media devices.
Then methods MediaDeviceList.getAudioList() and MediaDeviceList.getVideoList() are used to list available microphones and cameras.

line 88


line 91


The received lists of media devices are used to fill the corresponding drop-down lists.

3. Connection to server.

Session for connection to server is created when Start button is clicked. line 129

session = Flashphoner.createSession(sessionOptions);

Session is created with method createSession(), to which object SessionOptions (line 122) with the following parameters is passed

  • URL of WCS server
  • SurfaceViewRenderer, which will be used to display video from the camera
  • SurfaceViewRenderer, which will be used to play the published video stream

Callback functions for session events are added (line 134)

  • onConnected() - will be called when connection is successfully established
  • onDisconnection() - will be called when connection is closed
session.on(new SessionEvent() {
    public void onConnected(final Connection connection) {
    public void onDisconnection(final Connection connection) {

Method Session.connect() is called to establish connection with WCS server. line 253

session.connect(new Connection());

4. Video streaming.

After establishing connection to the server, new video stream is created with method Session.createStream(). line 167

publishStream = session.createStream(streamOptions);

Object StreamOptions (line 156) with name of the stream and VideoConstraints object is passed to the method.

Parameters of VideoConstraints object

  • selected camera - set with method VideoConstraints.setCameraId()
  • FPS - set with method VideoConstraints.setVideoFps()
  • resolution (width, height) - set with method VideoConstraints.setResolution()

To add сonstraints, method StreamOptions.setConstraints() is used. line 162

streamOptions.setConstraints(new Constraints(new AudioConstraints(), videoConstraints));

Callback function for processing stream statuses is added. (line 172)

publishStream.on(new StreamStatusEvent() {
    public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
        runOnUiThread(new Runnable() { 
            public void run() { 
                if (StreamStatus.PUBLISHING.equals(streamStatus)) {

Method Stream.publish() is called to publish the stream. line 224


5. Playback of video stream.

When the stream is published, new stream is created to play the published stream. line 188
Object StreamOptions (line 183) with name of the published stream is passed when the stream is created.

playStream = session.createStream(streamOptions);

Callback function for processing stream statuses is added. (line 193)

playStream.on(new StreamStatusEvent() {

Method is called to play the stream. line 211;

6. Disconnection. line 265

Method Session.disconnect() is called to close connection to the server.

  • No labels