Push SDK for Android

Updated at:
Copy as MD

Integrate the ApsaraVideo Live Push SDK into your Android app to capture camera and microphone input and push a live stream to an ingest endpoint.

How it works

The Push SDK handles the full pipeline from media capture to delivery:

  1. The SDK captures video from the camera and audio from the microphone.

  2. Your app calls the SDK to initialize a push stream with the ingest URL from ApsaraVideo Live.

  3. The SDK encodes and transmits the stream to the ingest endpoint in real time.

  4. Viewers connect to the playback URL to watch the live stream.

Prerequisites

Before you begin, make sure you have:

  • An Alibaba Cloud account with ApsaraVideo Live activated

  • An Android project targeting Android 5.0 (API level 21) or later

  • Android Studio 3.0 or later

  • A valid ingest URL from the ApsaraVideo Live console

Integrate the SDK

Add the Maven dependency

The Push SDK is distributed via Maven. Add the dependency based on your Android Gradle Plugin (AGP) version.

For AGP 7.1.0 or later, add the Maven repository in settings.gradle:

dependencyResolutionManagement {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/central' }
    }
}

For AGP earlier than 7.1.0, add the Maven repository in your project-level build.gradle:

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/central' }
    }
}
Note

If dependencyResolutionManagement is not present in settings.gradle, your AGP version is earlier than 7.1.0. Use the build.gradle approach instead.

Then add the SDK dependency in your app-level build.gradle:

dependencies {
    implementation 'com.aliyun.video.android:pusher:x.x.x'
}

Replace x.x.x with the latest SDK version. To find the current version, see the ApsaraVideo Live release notes.

Declare permissions

Add the following permissions to your AndroidManifest.xml:

<!-- Camera access for video capture -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Microphone access for audio capture -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- Network access for stream delivery -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network state detection for adaptive bitrate -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Wi-Fi state detection -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Read local media files -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Keep screen on during streaming -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Background streaming support -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

On Android 6.0 (API level 23) and later, CAMERA and RECORD_AUDIO are dangerous permissions and must be requested at runtime. Request them before starting the push stream.

Configure ProGuard

If your app uses code shrinking, add the following rules to your ProGuard configuration file:

-keep class com.alivc.** { *; }
-keep class com.aliyun.** { *; }
-dontwarn com.alivc.**
-dontwarn com.aliyun.**

Start a push stream

Initialize the pusher

Create an AlivcLivePusher instance and configure it with AlivcLivePushConfig:

Java

AlivcLivePushConfig config = new AlivcLivePushConfig();
config.setResolution(AlivcResolutionEnum.RESOLUTION_540P);
config.setFps(AlivcFpsEnum.FPS_20);
config.setEnableBitrateControl(true);

AlivcLivePusher pusher = new AlivcLivePusher();
pusher.init(context, config);

Kotlin

val config = AlivcLivePushConfig().apply {
    resolution = AlivcResolutionEnum.RESOLUTION_540P
    fps = AlivcFpsEnum.FPS_20
    isEnableBitrateControl = true
}

val pusher = AlivcLivePusher()
pusher.init(context, config)

Add a preview surface

Bind a SurfaceView to the pusher to display the camera preview:

Java

SurfaceView previewView = findViewById(R.id.preview_surface);
pusher.startPreview(previewView);

Kotlin

val previewView = findViewById<SurfaceView>(R.id.preview_surface)
pusher.startPreview(previewView)

Start and stop streaming

Call startPush with your ingest URL to begin streaming. Replace <your-ingest-domain> and <stream-name> with the values from your ApsaraVideo Live console.

Java

String ingestUrl = "rtmp://<your-ingest-domain>/live/<stream-name>";
pusher.startPush(ingestUrl);

Kotlin

val ingestUrl = "rtmp://<your-ingest-domain>/live/<stream-name>"
pusher.startPush(ingestUrl)

To stop the stream and release resources:

Java

pusher.stopPush();
pusher.destroy();

Kotlin

pusher.stopPush()
pusher.destroy()

Handle the pusher lifecycle

Call the following methods in your Activity lifecycle callbacks to avoid resource leaks:

Java

@Override
protected void onResume() {
    super.onResume();
    pusher.resumePush();
}

@Override
protected void onPause() {
    super.onPause();
    pusher.pausePush();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    pusher.stopPush();
    pusher.destroy();
}

Kotlin

override fun onResume() {
    super.onResume()
    pusher.resumePush()
}

override fun onPause() {
    super.onPause()
    pusher.pausePush()
}

override fun onDestroy() {
    super.onDestroy()
    pusher.stopPush()
    pusher.destroy()
}

Verify the stream

After calling startPush, confirm that the stream is live:

  1. Open the ApsaraVideo Live console.

  2. Go to Live Management > Streams.

  3. Confirm that your stream name appears in the active stream list.

Test playback using the playback URL in a media player or browser.

Next steps