Audiostream generator to audiofile with Flutter

65 views Asked by At

I use the package record in my flutter project and will generate an audio file from an audio stream via pcm16bit encoder. Just for now. Later I will send this stream to my speech-to-audio endpoint. This is my Codebase:

  Future<void> startRecording() async {
    try {
      if (await audioRecord.hasPermission()) {
        final file = File('/Users/steffen/Documents/steffen.wav');
        stream = await audioRecord.startStream(const RecordConfig(encoder: AudioEncoder.pcm16bits, echoCancel: true, noiseSuppress: true));
        stream.listen((data) {
           audioDataBuffer.addAll(data.toList());
          file.writeAsBytesSync(data, mode: FileMode.append);
          }, onDone: () => {
              sendFileToBackend()
          },
          onError: (e) => log(e.toString())
        );
        setState(() {
          isRecording = true;
        });
      }
    } catch (e) {
      log(e.toString());
    }
  }

Future<void> stopRecording() async {
    try {
      await audioRecord.stop();

      setState(() {
        isRecording = false;
      });

    } catch (e) {
      log(e.toString());

    }
  }

void sendFileToBackend() async {
    if(audioDataBuffer.isNotEmpty){
      Uri uri = Uri.parse('http://localhost:8000/speech-to-text/');
      var request = http.MultipartRequest('POST', uri);
      //var file = http.MultipartFile('file', stream, data.length);
      var file = http.MultipartFile.fromBytes(
          'file', audioDataBuffer, filename: 'speech.wav',
          contentType: MediaType('application', 'octet-stream'));

It is posible that I need a header for my stream?

0

There are 0 answers