How to Create a Live Streaming App with Flutter

Want to build a live streaming app that captivates audiences? Flutter can be your secret weapon. This guide will walk you through the process of creating a dynamic live streaming app using Flutter's powerful tools. We'll cover everything from setting up your development environment to implementing core features like video streaming, chat, and user interactions. Whether you're a seasoned developer or just starting out, this tutorial will equip you with the knowledge to bring your live streaming vision to life.

Building a Live Streaming App with Flutter

To create a live streaming app with Flutter, we'll leverage the power of ZEGOCLOUD. This platform offers a robust Live Streaming Kit that simplifies the development process. By handling complex tasks like video encoding, streaming, and delivery, ZEGOCLOUD allows you to focus on creating a unique user experience.

Before diving in, ensure you have the necessary tools:

  • A ZEGOCLOUD developer account - Create one

  • Obtain app credentials from the admin console

  • Flutter SDK installed on your system

  • Fundamental Dart programming skills

With these in place, you're ready to start building your live streaming app using Flutter and ZEGOCLOUD.

1. Setting Up the SDK

1.1 Adding Dependencies

To integrate the ZEGOCLOUD Live Streaming Kit, you must first add the necessary dependencies to your Flutter project. Open your terminal, navigate to your project's root directory, and execute the following command:

flutter pub add zego_uikit_prebuilt_live_streaming

This command updates your pubspec.yaml file, adding the required packages and ensuring that everything is ready for further development.

1.2 Importing the SDK

Once the dependencies are added, you'll need to import the ZEGOCLOUD Live Streaming Kit into your Dart file. Add the following import statement at the beginning of your Dart file:

import 'package:zego_uikit_prebuilt_live_streaming/zego_uikit_prebuilt_live_streaming.dart';

With this import, the necessary classes and methods from the SDK are now accessible in your code, allowing you to start building your live streaming features.

2. Project Configuration

To ensure your app works smoothly on both Android and iOS devices, follow the configuration steps below.

2.1 Android Setup

a. Modify build.gradle (app-level)

Navigate to android/app/build.gradle and make the following changes:

  • Kotlin Standard Library: Add the Kotlin standard library dependency to your project:
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
  • Minimum SDK Version: Set the minimum SDK version to 21 to ensure compatibility:
minSdkVersion 21
  • Compile SDK Version: If you're using Flutter 2.x.x, update the compile SDK version to 33:
compileSdkVersion 33

b. Update Kotlin Version

In the android/build.gradle file, specify the Kotlin version by adding:

buildscript {
    ext.kotlin_version = '1.8.0'
}

c. Add Required Permissions

To access the necessary device features, add the following permissions to your_project/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

These permissions are essential for live streaming functionalities like camera access, audio recording, and network usage.

d. Prevent Code Obfuscation

Create a file named proguard-rules.pro in the your_project/android/app directory with the following content to prevent code obfuscation:

-keep class .zego. { *; }

This ensures that the necessary classes from the ZEGOCLOUD SDK are not obfuscated during the build process.

e. Link ProGuard Rules in Build Configuration

In the same android/app/build.gradle file, link the ProGuard rules by adding this to the 'release' configuration:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

2.2 iOS Configuration

a. Podfile Configuration

Navigate to your_project/ios/Podfile and modify the post_install hook to include the following settings, ensuring that the necessary permissions are granted during the build process:

target.build_configurations.each do |config|
  config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
    '$(inherited)',
    'PERMISSION_CAMERA=1',
    'PERMISSION_MICROPHONE=1',
  ]
end

b. Update Info.plist

In your_project/ios/Runner/Info.plist, add the following keys to specify why your app requires access to the camera and microphone:

<key>NSCameraUsageDescription</key>
<string>We require camera access to connect</string>
<key>NSMicrophoneUsageDescription</key>
<string>We require microphone access to connect</string>

These descriptions will be displayed to users when the app requests permission to use these features.

3. Implementing the Live Streaming Kit

Now that the SDK is set up and your project is configured, it’s time to implement the live streaming functionality.

a. Obtain App Credentials

First, retrieve your appID and appSign from the ZEGOCLOUD Admin Console. These credentials are crucial for authenticating your app with the ZEGOCLOUD services.

b. Create the LivePage Widget

Define a new widget called LivePage, which will serve as the main interface for hosting and joining live streams:

class LivePage extends StatelessWidget {
  final String liveID;
  final bool isHost;

  const LivePage({Key? key, required this.liveID, this.isHost = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: ZegoUIKitPrebuiltLiveStreaming(
        appID: yourAppID, // Your ZEGOCLOUD App ID
        appSign: yourAppSign, // Your ZEGOCLOUD App Sign
        userID: 'user_id',
        userName: 'user_name',
        liveID: liveID,
        config: isHost
            ? ZegoUIKitPrebuiltLiveStreamingConfig.host()
            : ZegoUIKitPrebuiltLiveStreamingConfig.audience(),
      ),
    );
  }
}

Note:

  • userID, userName, liveID: Use only alphanumeric characters and underscores for these fields.

  • liveID: This identifier connects users to the same live stream. It’s unique per session.

  • Host Restrictions: Only one user can host a stream under a single liveID. All other users must join as audience members.

c. Integrate the LivePage Widget

To start a new live stream or join an existing one, navigate using the LivePage widget:

// Hosting a new live stream
Navigator.push(context, MaterialPageRoute(builder: (context) => LivePage(liveID: 'unique_live_id', isHost: true)));

// Joining an existing stream
Navigator.push(context, MaterialPageRoute(builder: (context) => LivePage(liveID: 'existing_live_id', isHost: false)));

4. Testing Your Implementation

After completing the setup and implementation:

  • Environment Setup: Ensure your Flutter development environment is correctly configured with all dependencies installed.

  • Device Connection: Connect a physical device or use an emulator/simulator for testing.

  • Build and Run: Use your IDE's build and run features to launch the app.

  • Testing Scenarios: Test the live streaming functionality by hosting and joining streams with different liveID values to ensure everything works as expected.

Conclusion

Building a live streaming app with Flutter, supported by ZEGOCLOUD, is a streamlined process that allows you to focus on creating a rich user experience. By following the steps outlined in this guide, you can successfully integrate essential live streaming features like video broadcasting, interactive chats, and seamless user interaction. With the right tools and a solid understanding of Flutter, you're now equipped to bring your live streaming vision to life, delivering a powerful and engaging app to your audience. Happy coding!