How to Build a Live Video Streaming iOS App with Agora
Recording and streaming a live feed from your app can be difficult, especially if you want to reach a global audience with little to no latency. The Agora Voice and Video SDK is perfect for reliably sending low-latency messages to a global audience, where you can have one or more people streaming their feeds.
In this tutorial, you learn how to create an application that enables users to be either a streamer or an audience member in a session. The setup is very similar to creating a video call with Agora, with a slight difference of roles: audience and broadcaster.
- An Agora Developer Account — Sign up here
- Xcode 12.0 or later
- iOS device with iOS 13.0 or later (as this project uses SF Symbols)
- A basic understanding of iOS development
- CocoaPods (if not using Swift Package Manager).
Note: If you need to target an earlier version of iOS, you can change how the buttons are created in the provided example project.
Let’s start with a new, single-view iOS project. Create the project in Xcode, then you can either install Agora RTC Engine in one of two ways:
Swift Package Manager
Swift Package Manager (SwiftPM) is the simplest method for installing dependencies to your apps, and is usually the preferred method for the same reason.
In your Xcode project, go to
File > Swift Packages > Add Package Dependency, and paste in the link to this repository:
To install with CocoaPods, you should go to the same directory as your Xcode project file in Terminal and run
Open the newly created
Podfile, and add the pod
AgoraRtcEngine_iOS in a similar way to this:
platform :ios, ‘13.0’
target ‘Project-Name’ do
pod ‘AgoraRtcEngine_iOS’, ‘~> 3.7.0’
pod init, and open the .xcworkspace file to get started.
If you want to jump ahead, you can find the full example project here:
This iOS example covers joining and leaving a channel, becoming a broadcaster, adding beautify camera effects, and…
Create the App
In this initial UIViewController, we can just have a button in the center to join the channel. This button will tell the application to open a view on top, enabling us to join as an audience member by default.
The view in this example is very basic, as you can see:
Next, create the
ChannelViewController.swift file. This file will contain our UIViewController subclass called ChannelViewController, which displays the Agora pieces.
In it, we will store values such as App ID, token, and channel name for connecting to the service, and add a button to change the user role between audience (default) and broadcaster. Also, initialise the Agora Engine with the correct client role. I have also preemptively added
userVideoLookup, which will keep track of the broadcasters/streamers.
userVideoLookup is set or updated,
reorganiseVideos is called to organise all of the streamed video feeds into a grid formation. Of course, a grid formation is not required, but if you want to implement the same thing, the
reorganiseVideos method is provided in the example project. Here’s a link to it:
Example of creating a multi-broadcasting channel with Agora.io - AgoraIO-Community/Agora-iOS-Swift-Example
The hostButton has a target set as
toggleBroadcast. This method is found right at the bottom of the gist. As you can see, it toggles the value of
.audience, and then sets the client role using
setClientRole. When the local client starts streaming, additional buttons should appear (for audio and video settings). But those buttons are displayed only after the client role has been changed, which is signaled by the delegate callback.
In the gist above, the ChannelViewController is set as the
AgoraRtcEngineDelegate, so we should add that protocol to our class, along with some callback methods.
As mentioned in the gist, the didClientRoleChanged is the callback for when we have changed the local user role to broadcaster or audience member.
If a remote host is streaming to us, the nonhosting view should now look like this:
When the user is streaming, they should have additional options, including the ability to switch between the device’s front- and back-facing cameras and turning the camera or microphone on and off. In this case, a beautification toggle is added. To show and hide these options, the
isHidden property of the button container is set to false or true, respectively.
In your application you may not want anyone to be able to start streaming. You could easily achieve this by requiring a password in the app (a static password or one authorised by a network request). Or you could offer separate apps for hosts and audience members.