Skip to content

[Android] Race condition: Native AudioRecord Error -38 and MPEG4Writer instability during record/stop cycle #482

@smakijenko

Description

@smakijenko

Describe the bug
Native AudioRecord errors (Error -38) and MPEG4Writer instability during the recording lifecycle. The application logs multiple Errors -38 during AudioRecord native read entries, suggesting that the native layer is attempting to read from the microphone after the session has been closed or before it's fully initialized. This leads to MPEG4Writer errors where the track is reported as "not started" when attempting to stop.

Issue occured only while using Android Device, on iOS there was not any problems.

//packages
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:audio_waveforms/audio_waveforms.dart';

//files
//manager
import '../managers/gemini_manager.dart';
import '../prompts/system_prompt_for_transcription.dart';

final descriptionModalViewModelProvider =
    ChangeNotifierProvider.autoDispose<DescriptionModalViewModel>((ref) {
      return DescriptionModalViewModel();
    });

class DescriptionModalViewModel extends ChangeNotifier {
  final RecorderController _recorderController = RecorderController();
  final TextEditingController textController = TextEditingController();
  bool _isListening = false;
  bool _isTrascribing = false;

  String get enteredText => textController.text;
  bool get isListening => _isListening;
  bool get isTrascribing => _isTrascribing;
  RecorderController get recorderController => _recorderController;

  void updateText() {
    notifyListeners();
  }

  Future<void> toggleRecording() async {
    try {
      if (!_isListening) {
        final hasPermission = await _recorderController.checkPermission();
        if (hasPermission) {
          _recorderController.reset();
          await _recorderController.record();
          _isListening = true;
          notifyListeners();
        }
      } else {
        final path = await _recorderController.stop();
        _isListening = false;
        notifyListeners();
        if (path != null) {
          await _transcribeAudio(path);
        } else {
          throw Exception('No file path.');
        }
      }
    } catch (e) {
      print("Recording error: $e");
    }
  }

  Future<void> _transcribeAudio(String filePath) async {
    try {
      _isTrascribing = true;
      notifyListeners();
      final transcription = await GeminiManager().transcribeAudio(
        systemPrompt: systemPromptForTranscription,
        filePath: filePath,
      );
      textController.text = transcription;
    } catch (e) {
      print('$e');
    } finally {
      _isTrascribing = false;
      notifyListeners();
    }
  }

  @override
  void dispose() {
    _recorderController.dispose();
    textController.dispose();
    super.dispose();
  }
}

Smartphone

  • Device: [POCO M4 Pro]
  • OS: [Android 11]
  • Version [MIUI Global 13.0.9]

Log.rtf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions