flutter桌面平台如何录音?

发布于 2025-01-11 04:39:50 字数 541 浏览 0 评论 0原文

我想在flutter桌面平台上录制语音。

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version 10.0.19042.1526], locale zh-CN)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.5)
[√] Android Studio (version 2020.3)
[√] IntelliJ IDEA Ultimate Edition (version 2020.3)
[√] Connected device (4 available)

I want to record voice on flutter desktop platform.

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version 10.0.19042.1526], locale zh-CN)
[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.5)
[√] Android Studio (version 2020.3)
[√] IntelliJ IDEA Ultimate Edition (version 2020.3)
[√] Connected device (4 available)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2025-01-18 04:39:50

使用 record 包,

步骤 1:声明用于存储录音数据的变量:

  int recordDuration = 0;
  Timer? _timer;
  final _audioRecorder = Record();
  StreamSubscription<RecordState>? _recordSub;
  RecordState recordState = RecordState.stop;
  String? audioPath;
  bool? isRecording;
  String? minutes = "00";
  String? seconds = "00";

步骤 2:创建启动方法:

initRecorder() async {
    _recordSub = _audioRecorder.onStateChanged().listen((recordStateThis) {
      recordState = recordStateThis;
    });
    audioPath = null;
    isRecorderInitialized = true;
    notifyListeners();
  }

第 3 步:创建用于启动、停止、恢复、暂停录制的方法:

  Future<void> _start() async {
    try {
      if (await _audioRecorder.hasPermission()) {
        final isSupported = await _audioRecorder.isEncoderSupported(
          AudioEncoder.aacLc,
        );
        if (kDebugMode) {
          print('${AudioEncoder.aacLc.name} supported: $isSupported');
        }
        // isRecording = await _audioRecorder.isRecording();
        await _audioRecorder.start();
        recordDuration = 0;
        _startTimer();
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
  }

  Future<void> _stop() async {
    _timer?.cancel();
    recordDuration = 0;
    final path = await _audioRecorder.stop();
    if (path != null) {
      onStop(path);
    }
  }


  Future<void> _pause() async {
    _timer?.cancel();
    await _audioRecorder.pause();
  }

  Future<void> _resume() async {
    _startTimer();
    await _audioRecorder.resume();
  }

第 4 步:声明启动计时器的方法:

  void _startTimer() {
    _timer?.cancel();
    _timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
      recordDuration++;
      minutes = _formatNumber(recordDuration ~/ 60);
      seconds = _formatNumber(recordDuration % 60);
      notifyListeners();
    });
  }

  String _formatNumber(int number) {
    String numberStr = number.toString();
    if (number < 10) {
      numberStr = '0$numberStr';
    }

    return numberStr;
  }

第 5 步:结束时处置录制器:

  disposeRecorder() {
    _timer?.cancel();
    _recordSub?.cancel();
    _audioRecorder.dispose();
    isRecorderInitialized = false;
    debugPrint("Dispose Success");
  }

在您的 ui 中使用这些方法。

Use record package,

Step 1: Declare variable for storing audio recording data:

  int recordDuration = 0;
  Timer? _timer;
  final _audioRecorder = Record();
  StreamSubscription<RecordState>? _recordSub;
  RecordState recordState = RecordState.stop;
  String? audioPath;
  bool? isRecording;
  String? minutes = "00";
  String? seconds = "00";

Step 2: Create initiate method:

initRecorder() async {
    _recordSub = _audioRecorder.onStateChanged().listen((recordStateThis) {
      recordState = recordStateThis;
    });
    audioPath = null;
    isRecorderInitialized = true;
    notifyListeners();
  }

Step 3: Create methods for starting, stopping, resuming, pausing recording:

  Future<void> _start() async {
    try {
      if (await _audioRecorder.hasPermission()) {
        final isSupported = await _audioRecorder.isEncoderSupported(
          AudioEncoder.aacLc,
        );
        if (kDebugMode) {
          print('${AudioEncoder.aacLc.name} supported: $isSupported');
        }
        // isRecording = await _audioRecorder.isRecording();
        await _audioRecorder.start();
        recordDuration = 0;
        _startTimer();
      }
    } catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
  }

  Future<void> _stop() async {
    _timer?.cancel();
    recordDuration = 0;
    final path = await _audioRecorder.stop();
    if (path != null) {
      onStop(path);
    }
  }


  Future<void> _pause() async {
    _timer?.cancel();
    await _audioRecorder.pause();
  }

  Future<void> _resume() async {
    _startTimer();
    await _audioRecorder.resume();
  }

Step 4: Declare method for starting timer:

  void _startTimer() {
    _timer?.cancel();
    _timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
      recordDuration++;
      minutes = _formatNumber(recordDuration ~/ 60);
      seconds = _formatNumber(recordDuration % 60);
      notifyListeners();
    });
  }

  String _formatNumber(int number) {
    String numberStr = number.toString();
    if (number < 10) {
      numberStr = '0$numberStr';
    }

    return numberStr;
  }

Step 5: Dispose recorder at end:

  disposeRecorder() {
    _timer?.cancel();
    _recordSub?.cancel();
    _audioRecorder.dispose();
    isRecorderInitialized = false;
    debugPrint("Dispose Success");
  }

Use these methods in your ui.

独木成林 2025-01-18 04:39:50

您是否尝试过可用的 flutter 软件包?如果没有,请尝试使用 flutter_desktop_audio_recorder记录。两者都支持Windows。

请按照自述文件部分了解实施细节。

Have you tried with the available flutter packages? If not, then try with flutter_desktop_audio_recorder or record. Both of them supports Windows.

Please follow the readme section for implementation details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文