翩翩。热重启会使应用崩溃,但冷重启不会(因为录音机已经被初始化了)。

问题描述 投票:0回答:1

我正在使用flutter_sound包来录制一些音频,只要应用程序启动,我就会初始化一个录音机.当我热重启应用程序时,另一个录音机被初始化,应用程序崩溃,因为在iOS上只能有一个录音机。

当我冷启动应用程序时,我没有遇到这个问题,可能是因为所有的资源都被释放了。我如何确保每当我热启动应用程序时,释放记录器的代码都会被调用?

这是UI中的相关代码。

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<RecorderService>(
          create: (_) => RecorderService(),
        ),
      ],
      child: MaterialApp(
        home: ScreenToShow(),
      ),
    );
  }

这是录音机服务类中的相关代码。

class RecorderService with ChangeNotifier {
  Recording recording;
  RecordingStatus status = RecordingStatus.uninitialized;
  static const String RECORDING_FORMAT = ".aac";
  static const String LISTENING_FORMAT = ".mp3";
  static const Duration UPDATE_DURATION_OF_STREAM = Duration(milliseconds: 100);

  RecorderService() {
    _initialize();
  }

  /// Private properties
  FlutterSoundRecorder _recorder;
  Directory _tempDir;
  FileConverterService _fileConverterService = FileConverterService();

  /// This is the file path in which the [_recorder] writes its data. From the moment it gets assigned in [_initialize()] it stays fixed
  String _pathToCurrentRecording;

  /// This is the file path to which the [recording] will be saved to. It changes with every call of [_startWithoutReset()]
  String _pathToSavedRecording;

  /// This function can only be executed once per session else it crashes on iOS (because there is already an initialized recorder)
  /// So when we hot restart the app this makes it crash
  _initialize() async {
    try {
      /// The arguments for [openAudioSession] are explained here: https://github.com/dooboolab/flutter_sound/blob/master/doc/player.md#openaudiosession-and-closeaudiosession
      _recorder = await FlutterSoundRecorder().openAudioSession(
          focus: AudioFocus.requestFocusAndKeepOthers,
          category: SessionCategory.playAndRecord,
          mode: SessionMode.modeDefault,
          audioFlags: outputToSpeaker);
      await _recorder.setSubscriptionDuration(UPDATE_DURATION_OF_STREAM);
      _tempDir = await getTemporaryDirectory();
      _pathToSavedRecording =
          "${_tempDir.path}/saved_recording$LISTENING_FORMAT";
      status = RecordingStatus.initialized;
      notifyListeners();
    } catch (e) {
      print("Recorder service could not be initialized because of error = $e");
    }
  }

  @override
  dispose() async {
    try {
      await _recorder?.closeAudioSession();
      super.dispose();
    } catch (e) {
      print("Recorder service could not be disposed because of error = $e");
    }
  }
}
flutter restart recorder hot-reload
1个回答
0
投票

你是否正确关闭了会话。阅读文档 此处


0
投票

我意识到这已经是你最初的帖子过去一个月了,但我今天遇到了这个问题。

我发现解决的方法是不调用下面的 initState() 的页面。

_recorder = await FlutterSoundRecorder().openAudioSession(...)

相反,我创建了以下内容:

Future<void> startAudioSession() async {recorderModule.openAudioSession(...);}

并把它写在页面的开头 startRecorder 函数,然后使用 closeAudioSession()stopRecorder 职能。

© www.soinside.com 2019 - 2024. All rights reserved.