Saving audio as a file

I recognise that code from somewhere :wink:

You can’t get the audio from the SpeechRecognition api.
But you can do it with the MediaStream Recording api alongside the SpeechRecognition api.

Adding the following to the button click

  def button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    from anvil.js.window import navigator, MediaRecorder, Blob
    stream = navigator.mediaDevices.getUserMedia({'audio': True})
    recorder = MediaRecorder(stream)
    chunks = []
    recorder.ondataavailable = lambda e: chunks.append(e.data)
    
    def onstop(e):
      anvil_media = anvil.js.to_media(chunks)
      ...
      
    recorder.onstop = onstop
    self.recorder = recorder

And then calling self.recorder.stop() at the same time you stop the speech recognition should work.

EDITED: since original post to reflect new anvi.js api