krosai_audio_client 0.1.0
krosai_audio_client: ^0.1.0 copied to clipboard
A Flutter package for implementing real-time audio communication using Krosai's audio infrastructure. Provides a simple interface for audio rooms, microphone control, and automatic reconnection handling.
Krosai Audio Client #
A Flutter package for implementing real-time audio communication in your Flutter applications using Krosai's audio infrastructure.
Features #
- Easy-to-use audio client
- Automatic permission handling
- Simplified room connection and management
- Microphone control
- Automatic reconnection handling
- Cross-platform support (Android, iOS, Web)
Installation #
Add the package to your pubspec.yaml
:
dependencies:
krosai_audio_client: ^0.1.0
Usage #
Basic Setup #
import 'package:krosai_audio_client/krosai_audio_client.dart';
final audioClient = KrosaiAudioClient();
// Connect to a room
await audioClient.connect(
url: 'wss://your-krosai-server.com',
token: 'your-token',
enableMicrophone: true,
);
// Disconnect when done
await audioClient.disconnect();
Example: Audio Chat Room #
Here's a complete example of implementing an audio chat room:
import 'package:flutter/material.dart';
import 'package:krosai_audio_client/krosai_audio_client.dart';
class AudioChatRoom extends StatefulWidget {
final String url;
final String token;
const AudioChatRoom({
Key? key,
required this.url,
required this.token,
}) : super(key: key);
@override
State<AudioChatRoom> createState() => _AudioChatRoomState();
}
class _AudioChatRoomState extends State<AudioChatRoom> {
final _audioClient = KrosaiAudioClient();
bool _isConnected = false;
bool _isMicrophoneEnabled = false;
@override
void initState() {
super.initState();
_connectToRoom();
}
@override
void dispose() {
_audioClient.disconnect();
super.dispose();
}
Future<void> _connectToRoom() async {
try {
await _audioClient.connect(
url: widget.url,
token: widget.token,
enableMicrophone: true,
);
setState(() {
_isConnected = true;
_isMicrophoneEnabled = true;
});
} catch (e) {
print('Failed to connect: $e');
}
}
Future<void> _toggleMicrophone() async {
try {
if (_isMicrophoneEnabled) {
await _audioClient.disableMicrophone();
} else {
await _audioClient.enableMicrophone();
}
setState(() {
_isMicrophoneEnabled = !_isMicrophoneEnabled;
});
} catch (e) {
print('Failed to toggle microphone: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Audio Chat Room'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Status: ${_isConnected ? 'Connected' : 'Disconnected'}',
style: Theme.of(context).textTheme.headline6,
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _toggleMicrophone,
child: Text(
_isMicrophoneEnabled ? 'Mute' : 'Unmute',
),
),
],
),
),
);
}
}
## Permissions
The package automatically handles the necessary permissions for microphone access. For Android, you need to add the following permissions to your `AndroidManifest.xml`:
```xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
For iOS, add the following to your Info.plist
:
<key>NSMicrophoneUsageDescription</key>
<string>This app needs microphone access for audio calls</string>
Connection Reliability #
The package implements recommended connection reliability features:
- Automatic reconnection on network changes
- Support for multiple connection types
- 30-second reconnection timeout
- Event handling for connection state changes
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments #
- LiveKit for the amazing real-time communication platform
- livekit_client for the Flutter client SDK