How to make flutter app to integrate both audio an...
# ask-ai
b
How to make flutter app to integrate both audio and text modality with livekit?
t
Thanks for reaching out, Rongrong. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you with integrating audio and text modalities in your Flutter app using LiveKit! Could you share a bit more about what you're aiming for? For instance: • Are you hoping to have real-time audio conversations alongside text chats? • Would you like to include features like transcribing audio to text or converting text to audio? • Are there any particular functionalities or experiences you want to create? The more details you can provide, the better I can support you on this journey.
b
yes, real-time audio conversations alongside text chats
t
Hi there! To help you enable real-time audio communication and text chat in your Flutter app using LiveKit, I've put together some easy steps for you to follow: 1. Install the LiveKit Flutter SDK First, add the
livekit_client
package to your
pubspec.yaml
file like this:
Copy code
dependencies:
  livekit_client: <latest_version>
Don’t forget to set up the necessary permissions for your platform: • iOS: Add camera and microphone permissions to your
Info.plist
and make sure to enable background audio. • Android: Include permissions for camera, microphone, and audio settings in your
AndroidManifest.xml
. 2. Connect to a Room Next, create a
Room
instance and connect using your server URL and access token:
Copy code
final room = Room();
await room.connect('<wss://your-server-url>', 'your-access-token');
Then, enable the microphone and camera for the local participant with these commands:
Copy code
await room.localParticipant?.setMicrophoneEnabled(true);
await room.localParticipant?.setCameraEnabled(true);
3. Enable Real-Time Audio Communication Good news—audio is enabled by default! But if you want to start audio playback explicitly, just do this:
Copy code
await room.startAudio();
For Android, make sure to initialize audio settings like this:
Copy code
import 'package:flutter_webrtc/flutter_webrtc.dart' as webrtc;

await webrtc.WebRTC.initialize(options: {
  'androidAudioConfiguration': webrtc.AndroidAudioConfiguration.communication.toMap()
});
4. Enable Text Chat To send chat messages, use
sendText()
like so:
Copy code
await room.localParticipant?.sendText('Hello!', options: SendTextOptions(topic: 'lk.chat'));
And to receive messages, listen for
DataReceivedEvent
like this:
Copy code
room.addListener((event) {
  if (event is DataReceivedEvent) {
    final text = utf8.decode(event.data);
    print('Received message: $text');
  }
});
5. Optional: Use Prebuilt UI Components If you want to speed up your development, the
livekit_components
package offers prebuilt widgets for chat and audio/video controls, which can be super handy. For more detailed info, feel free to check out the LiveKit Flutter SDK GitHub and the LiveKit Documentation. If you have any questions along the way, I’m here to help! Sources: lib/src/core/room.dart | lib/controllers/app_ctrl.dart | Docs::Agents | Web and mobile frontends | LiveKit Docs | example/lib/main.dart