Software developers at work.

Using OpenSDK Android RTMP Features

This article will demonstrate how you can use the various features of the Android RTMP OpenSDK tool.

Camera View

Make sure you call the start method on the camera view before streaming. The start method will enable the frames from the camera to get rendered to the user and will send the actual data to the encoder part.

If the start method is not called, the encoder will have no frames to publish.

mCameraView.start();

Start and Stop Publishing

To start publishing a video via Android RTMP, call the startPublish method on the StreamaxiaPublisher instance. The method requires the URL of the endpoint. Note that you need to provide your own media server or CDN, like AWS.

mStreamaxiaPublisher.startPublish(rtmpUrl);


To stop publishing just call the stopPublish method on the StreamaxiaPublisher instance.

mStreamaxiaPublisher.stopPublish();

Start, Stop, Pause and Resume Recording

To start recording (saving locally) the video, just call startRecord on the StreamaxiaPublisher instance.

mStreamaxiaPublisher.startRecord(recPath);


The method requires as a parameter the patch where to save the video. For example:

private static final String recPath = 
                       Environment.getExternalStorageDirectory().getPath() + "/test.mp4";

To stop the recording just call stopRecord on the object.

mStreamaxiaPublisher.stopRecord();


To pause the recording just call pauseRecord on the object.

mStreamaxiaPublisher.pauseRecord();


The difference between pause and stop is that when you call stopRecord, the video will be ended and saved locally on the given path. If you paused a video, you can resume (continue) the video with the resumeRecord method.

mStreamaxiaPublisher.resumeRecord();

Handling Configuration Changes (API 18+)

The Android RTMP SDK also supports orientation changes before the streaming is started. In order for this to be possible, you need to declare inside your Activity field in the AndroidManifest.xml file the following:

android:configChanges=“orientation|screenSize”

Then in the Activity override the on configuration changed method as follows:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    //Update the orientation for stream

    mStreamaxiaPublisher.setScreenOrientation(newConfig.orientation);

    //The new orientation might not support the current resolution

    Size supportedRes = mStreamaxiaPublisher.getSupportedPictureSizes(newConfig.orientation).get(0);

    mStreamaxiaPublisher.setVideoOutputResolution(supportedRes.widthsupportedRes.heightnewConfig.orientation);
}

By doing so, the user will be able to choose the orientation of the stream(portrait or landscape) by rotating the device, before the streaming is started

While streaming, the Activity will be locked on the current position and after finishing the streaming, it will be unlocked and the orientation can be changed again.

This feature can be disabled and you can choose only one orientation, as follows:

mStreamaxiaPublisher.setScreenOrientation(Configuration.ORIENTATION_LANDSCAPE);

 

or

mStreamaxiaPublisher.setScreenOrientation(Configuration.ORIENTATION_PORTRAIT);

By doing so, you will need to lock also the activity’s orientation from the AndroidManifest.xml file as follows:

android:screenOrientation=“landscape”

 

or

android:screenOrientation=“portrait”

You can also take screenshots at any point using the following code:

mCameraView.takeSnapshot(new CameraPreview.SnapshotCallback() {

     @Override

     public void onSnapshotTaken(Bitmap image) {

         //Do something with the snapshot

     }

 }); 

 

Realtime zooming in/out is also supported, you can use the following API:

// Get zoom range

int[] range = mCameraView.getZoomRange();

// Set max zoom

mCameraView.setZoom(range[1]);

Framerate and keyframe interval can be set BEFORE starting the Android RTMP stream using:

// Set the framerate to 30fps, we can also use the getter to check the value

mPublisher.setFramerate(30);

// Set the keyframe interval to 5 seconds, we can also use the getter to check the value

mPublisher.setKeyframeInterval(5);

NOTE : we do not support reverse landscape nor reverse portrait!

Share This Story, Choose Your Platform!