startPreview method
Future<void>
startPreview({
- required String imgset,
- required String streamKey,
- IvsQuality quality = IvsQuality.q720,
- CameraType cameraType = CameraType.BACK,
- void onData(
- dynamic
- void onError(
- dynamic
override
Starts the camera preview for the broadcast with the specified settings.
imgset
: The image set identifier for the broadcast.streamKey
: The stream key for the broadcast.quality
: The desired broadcast quality, default is IvsQuality.q720.cameraType
: The camera to use for the preview, default is CameraType.BACK.onData
: A callback function to handle real-time data from the event stream.onError
: A callback function to handle errors from the event stream.
Throws an Exception if the preview cannot start due to missing permissions, invalid parameters, or native platform issues.
Implementation
@override
Future<void> startPreview({
required String imgset,
required String streamKey,
IvsQuality quality = IvsQuality.q720,
CameraType cameraType = CameraType.BACK,
void Function(dynamic)? onData,
void Function(dynamic)? onError,
}) async {
try {
// Request permissions before starting the preview.
final permissionStatus = await requestPermissions();
if (!permissionStatus) {
throw Exception(
"Please Grant Camera and Microphone Permission [Start Preview]",
);
}
// Ensure imgset and streamKey are not empty.
if (imgset.isEmpty || streamKey.isEmpty) {
throw Exception('imgset or streamKey is empty [Start Preview]');
}
// Invoke the native platform method to start the preview.
await methodChannel.invokeMethod<void>('startPreview', <String, dynamic>{
'imgset': imgset,
'streamKey': streamKey,
'cameraType': cameraType.index.toString(),
"quality": quality.description,
});
// Cancel any existing event stream before starting a new one.
try {
eventStream?.cancel();
} catch (e) {
log("Error Cancelling Event Stream: $e");
}
// Start listening to the event stream for real-time data and errors.
eventStream = eventChannel
.receiveBroadcastStream()
.listen(onData, onError: onError);
} catch (e) {
throw Exception("$e [Start Preview]");
}
}