View & Play Live Stream
Host your events virtually and see community interaction as it happens
Live streams and playback videos information are stored in AmityStream. This stream objects reside in AmityStreamRepository. To start working with stream, first the app need to intialize the repository.
Swift
1
// `client` is AmityClient that has been initiated during the setup
2
streamRepository = AmityStreamRepository(client: client)
Copied!
*Note : There is a limitation for the maximum concurrent live events. Please feel free to reach out to us at community.amity.co with your use-case and we will determine if the current limit can be raised.

Retrieve a stream object

Each stream object has a unique identifier. To retrieve a single stream object, call
  • repository.getStreamById(_:)
This function returns a Live Object of AmityStream. The stream object contains essential data, for example, title and description.
1
let streamProxy = streamRepository.getStreamById(streamId)
2
3
getStreamToken = streamProxy.observe { (proxy, error) in
4
// We have a stream object here.
5
let stream = proxy.object
6
print("Title: \(stream.title)")
7
print("Description: \(stream.streamDescription)")
8
}
Copied!

Stream Status

Stream consists of many states. It can change from one state to another, depending on events and actions.
AmityStreamStatus represents a stream status. The following enum cases describe all the possible status of a stream.
  • .idle indicates "a stream that has generated but no actions have been taken."
  • .live indicates "a stream is currently being broadcasted."
  • .ended indicates "a stream has ended broadcasting and in the progress of transforming to a recorded stream."
  • .recorded indicates "a stream has ended broadcasting and has been transformed to a recorded stream."
You can check the status of a stream by calling .status.
1
// Print out the current status of a stream.
2
print(stream.status)
Copied!

Retrieve streams collection

To query streams collection, first you need to create a AmityStreamCollectionQuery.
1
// Create query object.
2
let query = AmityStreamCollectionQuery()
3
// Specify statuses to include in the streams collection.
4
query.includeStatus(.live)
Copied!
Then call .getStreamsCollection(from:) with the query object that you've created.
  • .getStreamsCollection(from: query)
This function returns the live collection of stream objects.
1
// Here we get AmityCollection<AmityStream>
2
streamsCollection = streamRepository.getStreamsCollection(from: query)
3
4
// An example of streams collection usage,
5
// to observe the changes and update UI.
6
token = streamsCollection.observe { [weak self] collection, change, error in
7
// Any update will be notified here.
8
self?.updateUI(from: collection)
9
}
Copied!
If your app needs stream collections in many parts of the app. We recommend to maintain only one collection for each query, in an application scope. And use it as a single source of truth.
1
App.liveStreamsCollections = streamRepository.getStreamsCollection(from: liveStreamQuery)
2
App.liveStreamsToken = App.liveStreamsCollection.observe { collection, change, error in
3
// Any update will be notified here.
4
App.updateDataSource(from: collection)
5
App.notifyLiveStreamsUpdate()
6
}
Copied!

Play a live stream

To play a live stream, currently only RTMP protocol is supported, call
  • stream.watcherUrl
AmityLiveStreamURLInfo contains a full RTMP url, which most of RTMP players support. For some players that does not support the full url, this object contains enough data for custom RTMP url formatting.
1
if let urlInfo = stream.watcherUrl {
2
3
// play with the full url
4
rtmpPlayer.play(urlInfo.url)
5
6
// or for some players that require custom RTMP url formatting
7
customRtmpPlayer.setHost("\(urlInfo.origin)/\(urlInfo.appName)?\(urlInfo.query)")
8
customRtmpPlayer.play(urlInfo.streamName)
9
10
}
Copied!
AmityStream has isLive property.
  • When there is an update of live object, use isLive property to check.
  • When there is an update of live stream collections:
    • The streams that are not live, will disappear from the collection.
    • The streams that are just live, will appear in the collection.
1
if (stream.isLive) {
2
print("The stream is live now.")
3
}
Copied!

Recommended RTMP players

RTMP is a low-latency video streaming protocol, that iOS does not support in its native video player. Therefore when working with RTMP, here are some open-source players that we recommend:

Play recorded videos

Live streams are recorded, and saved as files after the session ends. It would take some time for preparing recorded videos to be ready. You can observe the collection of stream that has recorded videos available, by calling
  • streamRepository.getRecordedStreams()
Each live stream session can contain multiple recorded videos. You can retrieve the array of AmityLiveVideoRecordingData that store all recording data, by calling
  • stream.recordingData
To get the actual url, you need to specify the file format by calling on a recorded item.
  • recordingItem.url(for: AmityLiveVideoRecordingFileFormat)
The following code shows an example of getting all the mp4 url of a stream instance.
1
// Print out all mp4 url for all recorded videos of `stream`.
2
//
3
for (index, dataItem) in stream.recordingData.enumerated() {
4
// Specify .MP4, to get the actual url in mp4 format.
5
if let url = dataItem.url(for: .MP4) {
6
print("Video \(index): \(url)")
7
} else {
8
print("Video \(index): url not found")
9
}
10
}
Copied!
In contrast with RTMP live videos, you don't need 3rd party video players for the recorded videos. iOS native players already support playing mp4 file from the URL given by API.
See also AVFoundation and AVKit.

Amity Video Player

Amity Video SDK includes AmityVideoPlayerKit.framework, a basic RTMP player to support live video functionality.
This framework requires MobileVLCKit.framework as a dependency. You can download it from the link below.