Broadcast Live Stream
To broadcast a live stream video, we provide a convenient broadcaster tool called AmityStreamBroadcaster. We now only support 16:9 video ratio with the following resolution representing by the enum AmityStreamBroadcastResolution:
  • .SD_480P indicates a video with resolution 480x854 and video bitrate 1216 kpbs
  • .HD_720P indicates a video with resolution 720x1280 and video bitrate 2496 kpbs
  • .FHD_1080P indicates a video with resolution 1080x1920 and video bitrate 4992 kpbs

Setup

You simply need to include this dependency to your project in build.gradle in the application level.
1
implementation 'com.github.AmityCo.Amity-Social-Cloud-SDK-Android:amity-video-publisher:x.y.z'
Copied!
Inside your Application class, in the application initialization process, you need to register the video publisher SDK to the core SDK by calling.
1
@Override
2
open fun onCreate() {
3
super.onCreate()
4
AmityStreamBroadcasterClient.setup(AmityCoreClient.getConfiguration())
5
}
Copied!
We highly recommend using Android's ConstraintLayout to construct our AmityCameraView Since we only support 16:9 video ratio, ConstratntLayout will ensure that the view will be drawn in the correct width and height.

Authorize the required permissions

AmityStreamBroadcaster requires the following permissions to work properly.
As per the required parameter by the aforementioned object to provide AmityCameraView , you will need to define the view in your Activity or Fragment layout by :
  1. 1.
    Camera access
  2. 2.
    Microphone access
Before using AmityStreamBroadcaster, please make sure these permissions are granted.
See here the steps to ask for the permissions.

Create an AmityStreamBroadCaster Object

In order to create the object, we also provide the AmityStreamBroadCaster.Builder class to create and configure this object easily. It requires AmityCameraView as a parameter and AmityStreamBroadcasterConfig as a configurable value which will be explained in the following section.
1
val broadcaster = AmityStreamBroadcaster.Builder(amity_camera)
2
.setConfiguration(broadcasterConfig)
3
.build()
Copied!

Prepare a configuration

For configuration, we provide the AmityStreamBroadcasterConfiguration.Builder to construct the configuration conveniently. As we have mentioned above, We support SD_480P, HD_720P and, FHD_1080P resolutions. Orientations are relying on Android Configuration class, you may either choose Configuration.ORIENTATION_PORTRAIT or Configuration.ORIENTATION_LANDSCAPE .
1
val configuration = AmityStreamBroadcasterConfiguration.Builder()
2
.setOrientation(Configuration.ORIENTATION_PORTRAIT)
3
.setResolution(AmityBroadcastResolution.HD_720P)
4
.build()
Copied!

Setup a layout

As per the required parameter by the aforementioned object to provide AmityCameraView , you need to define the view in your Activity or Fragment layout by :
1
<androidx.constraintlayout.widget.ConstraintLayout
2
android:layout_width="match_parent"
3
android:layout_height="match_parent">
4
5
<com.amity.socialcloud.sdk.video.AmityCameraView
6
android:id="@+id/amity_camera"
7
android:layout_width="0dp"
8
android:layout_height="0dp"
9
app:layout_constraintBottom_toBottomOf="parent"
10
app:layout_constraintDimensionRatio="H,9:16"
11
app:layout_constraintEnd_toEndOf="parent"
12
app:layout_constraintStart_toEndOf="parent"
13
app:layout_constraintStart_toStartOf="parent"
14
app:layout_constraintTop_toTopOf="parent"
15
app:layout_constraintVertical_bias="0.5">
16
</com.amity.socialcloud.sdk.video.AmityCameraView>
17
18
</androidx.constraintlayout.widget.ConstraintLayout>
Copied!
We highly recommend using Android's ConstraintLayout to construct our AmityCameraView Since we only support 16:9 video ratio, ConstratntLayout ensures that the view will be drawn in the correct width and height.

Preview the video

To begin previewing the camera input call
1
broadcaster.startPreview()
Copied!

Start live stream session

To begin broadcasting live stream call
1
broadcaster.startPublish(title: String, description: String)
Copied!

Stop live stream session

To stop broadcasting live stream call
1
broadcaster.stopPublish()
Copied!

Switch camera position

By default, the broadcaster will use the back camera. However, you can switch camera position anytime by calling.
1
broadcaster.switchCamera()
Copied!

Observe a broadcasting state

To observe the status of a broadcast, we provide a function (flow) to observe any status changes and return as AmityStreamBroadcasterState . The possible statuses are :
  • .IDLEindicates a status of stream in an idle state.
  • .CONNECTINGindicates a status of stream that it's connecting to a rtmp server.
  • .CONNECTEDindicates a status of stream that it's connected to a rtmp server.
  • .DISNNECTEDindicates a status of stream that it's disconnected to a rtmp server. We also provide error information through an exception.
1
broadcaster.stateFlowable
2
.subscribe { status ->
3
when (status) {
4
is AmitytreamBroadcasterState.CONNECTED { showConnected() }
5
is AmitytreamBroadcasterState.CONNECTING { showConnecting() }
6
is AmitytreamBroadcasterState.DISCONNECTED { showDisconnected()}
7
}
8
}
Copied!