As an industry, live streaming is already worth billions of dollars and it’s only expected to grow.
According to various projections, including one by Grand View Research, the live streaming industry is expected to climb from $70
billion in 2021 to almost $224
billion in 2028. That’s a projected three-fold increase over seven years!
With the rapid development of the live streaming industry, many technology companies have been derived to serve the live streaming industry by providing mature technologies to help users quickly build their own live streaming apps. Here I will introduce how to quickly build your own live streaming app by using ZEGOCLOUD's ZEGOLive SDK.
The ZEGOLive SDK provides all features that required by a live streaming app, such as creating and join a live stream room, co-hosting, face beautification, audio effects, virtual gifting, sending bullet-screen messages, and more. And all these features can be tried out by simply experiencing and integrating ZEGOCLOUD's Demo App for Live Streaming.
The following table shows ZEGOLive SDK's features:
The following diagram shows the basic process of creating a live room and a participant (user B) playing a stream published by the host (user A).
To integrate the SDK, do the following:
Download the Sample codes, and copy the ZEGOLive
folder to your project directory (create a new project if you don't have an existing project).
Add the Podfile
file to your project directory.
pod 'ZIM'
pod 'ZegoExpressEngine'
Open Terminal, run the install
command.
pod install
Permissions can be set as needed.
Privacy-Camera Usage Description
Privacy-Microphone Usage Description
To initialize the ZEGOLive SDK, get the RoomManager
instance, pass the AppID of your project.
// Initialize the SDK. We recommend you call this method when the application starts.
// YOUR_APP_ID is the AppID you get from ZEGOCLOUD Admin Console.
RoomManager.shared.initWithAppID(appID: YOUR_APP_ID) { result in
// Callback for the result of init.
};
To receive callbacks, set the corresponding delegate
to self
, or call the addUserServiceDelegate
method to listen for and handle event callbacks as needed.
RoomManager.shared.roomService.delegate = self
RoomManager.shared.userService.addUserServiceDelegate(self)
RoomManager.shared.messageService.delegate = self
To access the ZEGOLive service, you must log in first.
<div class="mk-warning">
Authentication
.let userInfo = UserInfo("YOUR_USER_ID", "YOUR_USER_NAME", .participant)
let token: String = "YOUR_TOKEN"
RoomManager.shared.userService.login(userInfo, token) { result in
// Callback for the login result.
}
Before creating a live room to start live streaming, you can call the playVideoStream
method to start the local video preview.
// The [userID] can be used to specify which user's view you want to view.
// To preview your own local video view, pass in your userID.
// streamView view is a view for the local video preview.
RoomManager.shared.deviceService.playVideoStream(userID, view: streamView)
co-host
to interact.<div class="mk-warning">
To create a live room, call the createRoom
method.
RoomManager.shared.roomService.createRoom("YOUR_ROOM_ID", "YOUR_ROOM_NAME", token) { result in
// Callback for the result of create a live room.
}
After a live room is created, to start live streaming, the host will need to call the takeSeat
method to speak. And the SDK automatically publishes the streams when the host takes a seat successfully.
RoomManager.shared.userService.takeSeat { result in
// Callback for the result of take a seat.
}
To join a live room, call the joinRoom
method.
RoomManager.shared.roomService.joinRoom("YOUR_ROOM_ID", token) { result in
// Callback for the result of join a live room.
}
After joining a live room, for a participant to watch the live streaming, he will need to call the playVideoStream
method to play the host's published streams.
// The [userID] can be used to specify which user's view you want to view.
// You can get the userID of the host in room info.
// streamView is the view to be displayed.
RoomManager.shared.deviceService.playVideoStream(userID, view: streamView)
To send text messages in the room, call the sendTextMessage
method.
RoomManager.shared.messageService.sendTextMessage("MESSAGE_CONTENT") { result in
// The result of send messages.
}
To receive the text messages, listen for the callback receiveTextMessage
.
func receiveTextMessage(_ message: TextMessage) {
// Implement the handling logic when receiving the text messages.
}
30 seconds before a Token expires, the SDK sends out a notification through the onRoomTokenWillExpire
callback.
Upon receiving this callback, you need to get a new Token from your app server first, and then pass the new token to the renewToken
method.
func onRoomTokenWillExpire(_ remainTimeInSecond: Int32, roomID: String?) {
let token: String = xxxxx ///new token
RoomManager.shared.roomService.renewToken(token, roomID: roomID)
}
Before the host leaves the live room, he will need to call the leaveSeat
to leave the seat first. And the SDK automatically stops publishing streams when the host leaves the seat successfully.
RoomManager.shared.userService.leaveSeat { Result in
// Callback for the result of leave a seat.
}
To leave the live room, call the leaveRoom
method. And the SDK stops all the stream publishing and playing operations simultaneously.
RoomManager.shared.roomService.leaveRoom { Result in
// Callback for the result of leave a live room.
}
To finish the ZEGOLive service, call the logout
method.
RoomManager.shared.userService.logout()
If you want to learn more about live broadcast-related technologies.
You can follow me or send me email
Also published here.