DEV Community

Stephen568hub
Stephen568hub

Posted on • Edited on

How to Create a Live Streaming App with Flutter.

Image description

Have you ever considered building your own live streaming app? Maybe you want to host live game streams, fitness classes, or even educational workshops. If so, then a Flutter live streaming app could be your golden ticket.

Flutter's a popular framework that lets you build sleek and powerful apps, and the good news is, it's surprisingly beginner-friendly for live streaming projects. This guide is here to hold your hand through the process, from setting up the basics to adding must-have features. By the end, you'll be equipped to turn your live streaming dream into a real app! Let's dive in!

How to Build a Live Streaming App with Flutter

In this section, we'll be building a live streaming app with Flutter! To make things faster, we'll be using ZEGOCLOUD UIKit. ZEGOCLOUD is a lifesaver for developers because it provides real-time audio and video communication tools. This means you won't have to write every line of code from scratch. Plus, you can take things up a notch with ZEGOCLOUD's fun features like minigames and customizable avatars. Now that we're all hyped up, let's jump right in and build our Flutter streaming app!

Prerequisites

  • A ZEGOCLOUD developer account - Sign up
  • Get app credentials from admin console
  • Have Flutter SDK installed
  • Basic knowledge of Dart programming language.

Now that you've met the prerequisites, let's walk through the process of creating your live streaming app with flutter. Follow these steps to get started:

1. Setting Up the SDK

1.1 Adding dependencies

To start using the ZEGOCLOUD Live Streaming Kit in your Flutter project, you need to add the necessary dependencies. Open a terminal, navigate to your project's root directory, and run the following command:

flutter pub add zego_uikit_prebuilt_live_streaming
Enter fullscreen mode Exit fullscreen mode

This will update your pubspec.yaml file and fetch the required packages.

1.2 Importing the SDK

After adding the dependencies, import the Live Streaming Kit SDK by adding the following import statement at the top of your Dart file:

import 'package:zego_uikit_prebuilt_live_streaming/zego_uikit_prebuilt_live_streaming.dart';
Enter fullscreen mode Exit fullscreen mode

Now, you're ready to use the ZEGOCLOUD Live Streaming Kit in your code.

2. Project Configuration

2.1 Android Setup

To ensure smooth operation on Android devices, make the following changes:

a. In android/app/build.gradle:

  • Add Kotlin standard library to dependencies:
dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
Enter fullscreen mode Exit fullscreen mode
  • Set minimum SDK version:
minSdkVersion 21
Enter fullscreen mode Exit fullscreen mode
  • For Flutter 2.x.x users, update compile SDK version:
compileSdkVersion 33
Enter fullscreen mode Exit fullscreen mode

b. Update android/build.gradle with the Kotlin version:

buildscript {
  ext.kotlin_version = '1.8.0'
}
Enter fullscreen mode Exit fullscreen mode

c. Add necessary 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" />
Enter fullscreen mode Exit fullscreen mode

d. Prevent code obfuscation by creating your_project/android/app/proguard-rules.pro with the following content:

-keep class **.zego.** { *; }
Enter fullscreen mode Exit fullscreen mode

e. In your_project/android/app/build.gradle, add the following to the 'release' configuration:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
Enter fullscreen mode Exit fullscreen mode

2.2 iOS Configuration

For iOS devices, follow these steps:

a. In your_project/ios/Podfile, add to the post_install hook:

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

b. Update your_project/ios/Runner/Info.plist with permission descriptions:

<key>NSCameraUsageDescription</key>
<string>We require camera access to connect</string>
<key>NSMicrophoneUsageDescription</key>
<string>We require microphone access to connect</string>
Enter fullscreen mode Exit fullscreen mode

3. Implementing the Live Streaming Kit

To integrate the Live Streaming Kit into your app:

a. Retrieve your appID and appSign from the ZEGOCLOUD Admin Console.

b. Create a LivePage widget:

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(),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Key points to remember:

  • Use only numbers, letters, and underscores for userID, userName, and liveID.
  • The same liveID connects users to the same live stream.
  • Only one host is allowed per liveID. Other users must join as audience members.

c. Utilize the LivePage widget in your app:

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

// Join an existing stream
Navigator.push(context, MaterialPageRoute(builder: (context) => LivePage(liveID: 'existing_live_id', isHost: false)));
Enter fullscreen mode Exit fullscreen mode

4. Testing Your Implementation

Once you've completed the setup:

  • Ensure your Flutter development environment is properly configured.
  • Connect a physical device or launch an emulator/simulator.
  • Use your IDE's Run or Debug function to build and launch the app.
  • Test both hosting and joining scenarios with various liveID values.

Conclusion

You've just learned how to create a live streaming app using Flutter and ZEGOCLOUD UIKit. By following this guide, you've set up the development environment, configured your project for both Android and iOS, and implemented the core functionality of your app.

Remember, this is just the beginning. You can now explore additional features like chat, virtual gifts, or screen sharing to enhance your app further. As you continue to develop and refine your live streaming app, keep experimenting with Flutter's capabilities and ZEGOCLOUD's offerings. With practice and creativity, you'll be able to create a unique and engaging live streaming experience for your users.

Want to build something truly exciting? Sign up to get started with ZEGOCLOUD!

Top comments (0)