transcribeFile method
Performs audio transcription with the ability to wait for the result
file - audio file for transcription
language - audio language (optional)
options - additional transcription options
waitForResult - wait for transcription completion
pollInterval - transcription status polling interval in milliseconds
maxAttempts - maximum number of polling attempts
If waitForResult = true, the method will wait for transcription completion.
Otherwise, it will return TranscriptionInitResult with URL and ID for getting the result later.
Implementation
Future<dynamic> transcribeFile({
required File file,
String? language,
TranscriptionOptions? options,
bool waitForResult = true,
int pollInterval = 1000,
int maxAttempts = 60,
}) async {
try {
// Step 1: Upload file
final uploadResult = await uploadAudioFile(file);
// Step 2: Initiate transcription
final transcriptionInit = await initiateTranscription(
audioUrl: uploadResult.audioUrl,
language: language,
options: options,
);
// If no need to wait for result, return initialization data
if (!waitForResult) {
return transcriptionInit;
}
// Step 3: Wait for transcription result
int attempts = 0;
while (attempts < maxAttempts) {
try {
final result =
await getTranscriptionResult(transcriptionInit.resultUrl);
return result; // Successfully got result
} on GladiaApiException catch (e) {
// If status 202, transcription not completed yet
if (e.statusCode == 202) {
// Wait before next attempt
await Future.delayed(Duration(milliseconds: pollInterval));
attempts++;
} else {
// Other errors propagate further
rethrow;
}
}
}
// If maximum attempts exceeded, return error
throw GladiaApiException(
message: 'Maximum waiting time for transcription result exceeded',
statusCode: 408,
);
} catch (e) {
if (e is GladiaApiException) {
rethrow;
}
throw GladiaApiException(message: e.toString());
}
}