convertAudio method

  1. @override
Future<ConversionResult> convertAudio({
  1. required String inputPath,
  2. required String outputPath,
  3. required AudioFormat format,
  4. int bitRate = 128,
  5. int sampleRate = 44100,
  6. ProgressCallback? onProgress,
})
override

Converts an audio file to the specified format

Supports converting between various audio formats with customizable quality settings. Uses native platform APIs for optimal performance and compatibility.

inputPath - Path to the input audio file outputPath - Path where the converted file will be saved format - Target audio format (see AudioFormat) bitRate - Target bit rate in kbps (default: 128) sampleRate - Target sample rate in Hz (default: 44100) onProgress - Optional callback for conversion progress (0.0 to 1.0)

Returns ConversionResult with output file information. Throws AudioConversionException if conversion fails.

Implementation

@override
Future<ConversionResult> convertAudio({
  required String inputPath,
  required String outputPath,
  required AudioFormat format,
  int bitRate = 128,
  int sampleRate = 44100,
  ProgressCallback? onProgress,
}) async {
  // Validate arguments
  _validateFilePath(inputPath, 'inputPath');
  _validateFilePath(outputPath, 'outputPath');
  _validateBitRate(bitRate);
  _validateSampleRate(sampleRate);

  // Set up progress listening
  StreamSubscription<dynamic>? progressSub;
  if (onProgress != null) {
    progressSub = _progressChannel.receiveBroadcastStream().listen((data) {
      if (data is Map && data['operation'] == 'convert') {
        final progress = data['progress'] as double?;
        if (progress != null) {
          onProgress(progress);
        }
      }
    });
  }

  try {
    final Map<String, dynamic> arguments = {
      'inputPath': inputPath,
      'outputPath': outputPath,
      'format': format.name,
      'bitRate': bitRate,
      'sampleRate': sampleRate,
    };

    final result = await methodChannel
        .invokeMethod('convertAudio', arguments)
        .timeout(const Duration(minutes: 10));

    if (result == null) {
      throw AudioConversionException(
        'Conversion failed: No result returned from platform',
        inputPath: inputPath,
        outputPath: outputPath,
        targetFormat: format.name,
      );
    }

    // Safely convert the result to Map<String, dynamic>
    final Map<String, dynamic> convertedResult;
    if (result is Map) {
      convertedResult = Map<String, dynamic>.from(result);
    } else {
      throw AudioConversionException(
        'Conversion failed: Invalid result type from platform',
        inputPath: inputPath,
        outputPath: outputPath,
        targetFormat: format.name,
      );
    }

    return ConversionResult(
      outputPath: convertedResult['outputPath'] as String,
      durationMs: convertedResult['durationMs'] as int,
      bitRate: convertedResult['bitRate'] as int,
      sampleRate: convertedResult['sampleRate'] as int,
    );
  } on PlatformException catch (e) {
    throw _convertPlatformException(e, AudioConversionException.new, {
      'inputPath': inputPath,
      'outputPath': outputPath,
      'targetFormat': format.name,
    });
  } catch (e) {
    throw AudioConversionException(
      'Conversion failed: $e',
      originalError: e,
      inputPath: inputPath,
      outputPath: outputPath,
      targetFormat: format.name,
    );
  } finally {
    await progressSub?.cancel();
  }
}