Broadcast Live Stream
To broadcast a live stream video, we have provided a convenient broadcaster tool called AmityStreamBroadcaster. You will need to import AmityLiveVideoBroadcastKit.framework into your app.

Authorize the required permissions

AmityStreamBroadcaster requires the following permissions to work properly.
  1. 1.
    Camera access
  2. 2.
    Microphone access
Before using AmityStreamBroadcaster, please make sure these permissions are already granted.
Here are the steps to ask for the permissions.
  1. 1.
    Add NSCameraUsageDescription to your app's info.plist.
  2. 2.
    Add NSMicrophoneUsageDescription to your app's info.plist.
  3. 3.
    Call AVCaptureDevice.requestAccess(for: .video, completion: ...)
  4. 4.
    Call AVAudioSession.sharedInstance().requestRecordPermission(_:)

Create an AmityStreamBroadcaster

You can create broadcaster instance by providing AmityClient.
1
broadcaster = AmityStreamBroadcaster(client: client)
Copied!

Prepare a configuration

Before going live, you will need to set up the broadcaster session by creating AmityStreamBroadcasterConfiguration. This configuration will be applied for the next live session.
1
// Prepare configs
2
let config = AmityStreamBroadcasterConfiguration()
3
config.canvasFitting = .fill
4
config.bitrate = 3_000_000 // 3mbps
5
config.frameRate = .fps30
6
7
// Setup the next live session with the given configs.
8
broadcaster.setup(with: config)
Copied!

Preview the video

The broadcaster object provides "preview view". After finish setting up the broadcaster, you then can take the preview view to attach into your screen.
1
// Attach the preview view to show on the screen.
2
myView.addSubview(broadcaster.previewView)
Copied!

Setup video resolution

You can set up video resolution anytime "before" going live. This setting will affect both the rendering of preview view, and the resolution of live stream session.
1
// This settings apply the following changes.
2
// 1. The preview view to render at 720p.
3
// 2. The live stream session to broadcast at 720p.
4
broadcaster.videoResolution = CGSize(width: 1280, height: 720)
Copied!

Start live stream session

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

Stop live stream session

To stop broadcasting the 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
// Use the back camera
2
broadcaster.cameraPosition = .back
3
4
// Use the front camera
5
broadcaster.cameraPosition = .front
Copied!

Observe a broadcaster state

To observe a broadcaster state, we provide a delegate object that you can set to listen to broadcaster's events.
1
broadcaster.delegate = self
Copied!
AmityStreamBroadcasterState represents all the possible states.
  • .idle indicates a state of stream in an idle state.
  • .connecting indicates a state of stream that it's connecting to the stream server.
  • .connected indicates a state of stream that it's connected to the stream server.
  • .disconnected indicates a status of stream that it's disconnected.
The code below shows an example of printing out the state of broadcaster, when it changes.
1
extension MyViewController: AmityStreamBroadcasterDelegate {
2
3
func amityStreamBroadcasterDidUpdateState(_ broadcaster: AmityStreamBroadcaster) {
4
print(broadcaster.state)
5
}
6
7
}
Copied!