【问题标题】:FLUTTER: I want to record the stream of microphone and play it immediatelyFLUTTER:我想录制麦克风流并立即播放
【发布时间】:2019-04-17 11:12:21
【问题描述】:

我想做和应用程序谁获取麦克风流并直接在 Flutter 中播放它,谁可以帮助我,我在互联网上找不到任何东西。谢谢!

【问题讨论】:

    标签: audio dart flutter microphone


    【解决方案1】:

    经过一些更改后,它的工作方式是这样的,请使用这个插件: https://pub.dev/packages/sound_stream

    将此导入您的 pubspec:

    dependencies:
      sound_stream: ^0.2.0
    

    并使用此示例代码:

    import 'dart:async';
    import 'dart:typed_data';
    import 'package:flutter/material.dart';
    
    import 'package:sound_stream/sound_stream.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      RecorderStream _recorder = RecorderStream();
      PlayerStream _player = PlayerStream();
    
      List<Uint8List> _micChunks = [];
      bool _isRecording = false;
      bool _isPlaying = false;
    
      StreamSubscription _recorderStatus;
      StreamSubscription _playerStatus;
      StreamSubscription _audioStream;
    
      @override
      void initState() {
        super.initState();
        initPlugin();
      }
    
      @override
      void dispose() {
        _recorderStatus?.cancel();
        _playerStatus?.cancel();
        _audioStream?.cancel();
        super.dispose();
      }
    
      // Platform messages are asynchronous, so we initialize in an async method.
      Future<void> initPlugin() async {
        _recorderStatus = _recorder.status.listen((status) {
          if (mounted)
            setState(() {
              _isRecording = status == SoundStreamStatus.Playing;
            });
        });
    
        _audioStream = _recorder.audioStream.listen((data) {
          if (_isPlaying) {
            _player.writeChunk(data);
          } else {
            _micChunks.add(data);
          }
        });
    
        _playerStatus = _player.status.listen((status) {
          if (mounted)
            setState(() {
              _isPlaying = status == SoundStreamStatus.Playing;
            });
        });
    
        await Future.wait([
          _recorder.initialize(),
          _player.initialize(),
        ]);
      }
    
      void _play() async {
        await _player.start();
    
        if (_micChunks.isNotEmpty) {
          for (var chunk in _micChunks) {
            await _player.writeChunk(chunk);
          }
          _micChunks.clear();
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                IconButton(
                  iconSize: 96.0,
                  icon: Icon(_isRecording ? Icons.mic_off : Icons.mic),
                  onPressed: _isRecording ? _recorder.stop : _recorder.start,
                ),
                IconButton(
                  iconSize: 96.0,
                  icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
                  onPressed: _isPlaying ? _player.stop : _play,
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 对于 iOS 有这个错误:Xcode's output: ld: warning: Could not find or use auto-linked library 'swiftDispatch' ld: warning: Could not find or use auto-linked library 'swiftCoreMedia' ld: warning: Could not find or use auto-linked library 'swiftAVFoundation' ld: warning: Could not find or use auto-linked library 'swiftFoundation' ld: warning: Could not find or use auto-linked library 'swiftsimd' ld: warning: Could not find or use auto-linked library 'swiftCompatibility51' 等。知道为什么吗?
    【解决方案2】:

    flutter_sound(githubdocs) 库似乎有很好的工具来处理音频和流。

    甚至还有一个example page for streams,它准确地显示了如何录制到流和回放。您只需将依赖项添加到您的pubspec.yaml 并为平台权限文件添加必要的权限,然后链接的示例将独立作为一个页面,同时具有记录器和播放器来演示功能。

    【讨论】:

      【解决方案3】:

      您可以使用this 库从麦克风录制音频。

      用法 要使用此插件,请在 pubspec.yaml 文件中添加 audio_recorder 作为依赖项。

      安卓

      <uses-permission android:name="android.permission.RECORD_AUDIO" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      

      iOS

      <key>NSMicrophoneUsageDescription</key>
      <string>Record audio for playback</string>
      

      示例

      // Import package
      import 'package:audio_recorder/audio_recorder.dart';
      
      // Check permissions before starting
      bool hasPermissions = await AudioRecorder.hasPermissions;
      
      // Get the state of the recorder
      bool isRecording = await AudioRecorder.isRecording;
      
      // Start recording
      await AudioRecorder.start(path: _controller.text, audioOutputFormat: AudioOutputFormat.AAC);
      
      // Stop recording
      Recording recording = await AudioRecorder.stop();
      print("Path : ${recording.path},  Format : ${recording.audioOutputFormat},  Duration : ${recording.duration},  Extension : ${recording.extension},");
      

      现在您需要做的就是播放录制的声音文件。

      【讨论】:

        猜你喜欢
        • 2019-03-01
        • 1970-01-01
        • 2011-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多