How to play Audio from an Audio file using anvil

I would probably thank David for his extensive support, continue to debug and research on my own, and hope that someone else will jump in to help :slight_smile:

1 Like

I’ll keep looking but I can’t promise anything as I too am now stumped.

Oh Yeah… I never expected such support…its my first time using forum and you have been a lot of help, David. Thank You

2 Likes

Just a hunch, but does it make any difference if you close the BytesIO instance before returning?

This works. I’m just saving the mp3 and then reading it back in.

import anvil.server
from gtts import gTTS
from io import BytesIO
import os

@anvil.server.callable
def speak(audio_string):
    mp3_fp = BytesIO()
    tts = gTTS(audio_string, lang='en')
    tts.write_to_fp(mp3_fp)

    tts.save("my_file.mp3")

    with open('my_file.mp3', 'rb') as f:
        data=f.read()

    tts_blob = anvil.BlobMedia('audio/mpeg', data, name='audio.mp3')
    os.remove('my_file.mp3')

    return tts_blob
1 Like

Oh Yeah It works thank u soooo much Campopianoa :smiley: :smiley: :smiley: :smiley:…but it is downloading the file but not playing it instantly…also is there a way we can delete it right after it speaks. David told me it would automatically happen but anyways.

update above now includes deleting the file.

Oh sorry i didn’t see that :sweat_smile:.

I updated it after you asked so no worries!

Okay… But again, is there a way for it to play automatically :thinking:

It may be best to post that question to a new topic. Also you may Google to see if you can find more information now that you have this aspect working correctly.

Okay, Campopianoa…Again, Thank you sooo much David & Campopianoa …

2 Likes

I stood on @david.wylie’s shoulders for this one. He organized things properly so that debugging and research was made easy.

1 Like

Great discussion about audio files generated on Colab. But the piece that is missing for me is how to play the audio file back in the Anvil code. I used anvil.media.download(tts_blob), and it prompted me to download the file (which played fine when opened). But how would you play the audio automatically in the browser?

There are a few examples littering the forum, eg :

As this is an old, solved post please can you start a new topic (you can link to this one if you want)?

1 Like