Build a Live-Streaming Broadcast App for AndroidSeptember 4, 2019
From live commerce to e-gaming, live-streaming apps attract users across a range of industries. There’s no better way to engage users in 2019. But streaming can be complicated, even for experienced developers.
In this video, Justin shows how to build a playback app using the GoCoder SDK for Android. This free tool simplifies mobile app development by offering a common, cross-platform API to capture, encode, and stream live video.
Please reference our docs to follow the coding process.
Full Video Transcript:
Let’s look at how to build a basic live streaming broadcast app for Android using Android Studio, Wowza GoCoder SDK, and Wowza Streaming Cloud as our media server and content delivery network. From wowza.com, you can get a free trial for Wowza Streaming Cloud and sign up for a free Wowza GoCoder SDK license. When signing up, be sure to use a reverse domain name for the application ID. Both Apple and Google prefer it. If you already have your license, you can download Wowza GoCoder SDK using the download link at the top, then selecting the Wowza GoCoder SDK tab. Once you’ve downloaded that installer for Android, be sure to unzip the file somewhere you’ll remember.
Now, to start building, we’ll be using the Wowza documentation, build a live streaming app with GoCoder SDK for Android. Open Android Studio and start a new project. The project will use an empty activity. Name your project, then add your application ID as your package name. Make sure you know the location your project will be saved in, set the language to Java, and the API level to at least 21. Once you’re finished, your project will load in Android Studio. Now we’ll need to configure the project to use Wowza GoCoder SDK. Locate the unzipped Wowza GoCoder SDK directory. You’ll need to copy the .aar file. Then access where your Android is being saved. Under app, in the libs folder, paste the .aar file. This will make it easier to reference the SDK.
In Android Studio, open build.gradlemoduleapp. From the install documentation, copy the code to locate the libs folder, and paste this before the dependencies. Then, back in the documentation, you’ll also want to copy the GoCoder SDK library dependency declaration and paste it into the dependencies. Once you’re done, you can sync the project. Now we can go to the manifest and, from here, we’ll add permissions from the install documentation to access the camera, record audio, internet, flashlight, and audio settings. Paste them above the application block. With the SDK configuration complete, we can start building the basic app by returning to the main documentation and adding a camera preview and broadcast button to the layout definition file. Copy the code, and then back in Android Studio, go to res and under layout, open activity_main.xml.
Using the text tab, you’ll replace its contents by pasting in the new code. In order to hide the keyboard, we’ll go back to the documentation and copy the Android config changes property. Then returning to the AndroidManifest.xml file, we’ll paste over the activity element. Now we can enable Android’s immersive, sticky, fully screen mode by copying the code in the documentation, then heading over to the main activity class, and pasting it in before the last curly brace. You may see a few sections turn red. Use Option>Return to connect them to their dependencies. Next, we’ll define app properties. To make them global, copy the code in the documentation and then paste them over the main activity. Again, we’ll need to use Option>Return to build them in properly. Back in the documentation, we’ll continue by registering and initializing the SDK.
Copy the code, which will need to be amended with the correct license key. Now, you should have received your license key via email. Be sure to save this somewhere safe. The license key is specific to your application ID. Back in Android Studio, paste this into the end of the OnCreate method, implementing the correct license key as you do so. You’ll also need to use Option>Return to connect to their dependencies. Returning back to the documentation, we’ll check for app permissions. Once again, copy the code and we’ll paste this in before the last curly brace to grant permissions to access the camera and microphone. This code will have quite a number of areas where you’ll need to use Option>Return. When you’re finished, we’ll go back to the documentation to start the camera preview. We’ll copy the first part of the code, pasting it into the on create method just below the SDK initialization to associate the view defined in the activities layout.
Then, back in the documentation, we’ll copy the second part, which we’ll need to add to the OnResume method to turn on the camera preview when the app is brought to the foreground. Now we can add and configure a broadcast. For this, we’ll start by creating the audio device. Copy the code, and then you’ll paste the code in the OnCreate method to create the audio device instance. Back in the documentation, we’ll copy the code to configure the broadcast connection to our media server, Wowza Streaming Cloud. This will be pasted into the OnResume method. You’ll need to use Option>Return to build in the Wowza media config. This section requires a number of settings from Wowza Streaming Cloud to work, including a source user name and password. While there are many cases where this is not required, you can easily copy and paste this from our documentation.
The second to last part we’ll need to add is the broadcast monitoring callbacks. The first section will need to be added to the main activity class definition to monitor for status updates and errors during broadcast. In this one instance, we will not use Option>Return for Wowza status call back. Back in the documentation, copy the defined methods, which will be pasted into the main activity class before the final curly brace. These will need quite a number of Option>Returns to build them in properly. Now we can add the last step to start streaming. Add the ViewOnClick.listener interface to the main activity class definition. Once this is pasted in, we can use Option>Return to build in Wowza status call back. Then, back in the documentation, we’ll copy the code, add an onClick() event handler. This will be pasted into the bottom, before the last curly brace of the main activity class to be invoked when the Broadcast button is tapped. Use Option>Return to build it in properly.
Last, we’ll associate the onClick() event handler with the Broadcast button defined in the activities layout. This will be pasted into the bottom of the on create method, in the main activity class. Use Option>Returns once more to build it in properly. That’s it. We could deploy the build, but we still have to go to cloud.wowza.com and set up the live stream in Wowza Streaming Cloud. From Live Streams, at the top, select Add Live Stream. We’ll name this live stream, select a location closest to where we plan to broadcast, and click Next. On the following screen, we’ll choose Wowza GoCoder as our encoder, and change the aspect ratio to 1920 by 1080. Click Next and continue to click Next through the following screens until we’re finished. Most of these settings can be modified later. With the live stream created, we can scroll down the overview and locate the source connection information.
We’ll need to copy this information as our connection properties in Android Studio. While the primary server URL needs to be broken up into host address and application name, everything else should be easy to follow. In the future, you can use the API to pull this type of information from Wowza Streaming Cloud. Once you’re finished with these changes, you can deploy the build to a device with the minimum API level. Before you attempt to broadcast, you’ll need to go back into Wowza Streaming Cloud and Start Live Stream. Once the live stream has started and running, you can then test the app and verify its broadcasting. While you can view the live stream from a player or hosted page, you can also see it’s working from the video thumbnail, statistics, and health monitoring. When you’re finished, be sure to stop your live stream in Wowza Streaming Cloud.
For more information on developing a broadcast app using Wowza GoCoder SDK, be sure to check out our additional documentation. Thanks for watching and happy streaming.