[Python] Python, need to play audio extracted from a text-to-speech API, but I cannot convert...

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    For my program I need to have a field of text spoken by Google's text to speech.

    I found an API that manages to do it well (gTTS, docs), however I really need to find a way to play the audio without saving it as a local file. My code at the moment is this (I imported AudioSegment from pydub):

    FileToPlay = io.BytesIO()
    tts = gTTS(SecondText, lang = SecondLanguageVoice)
    tts.write_to_fp(FileToPlay)
    FileToPlay.read()
    SoundToPlay = AudioSegment.from_mp3(io.BytesIO(FileToPlay))
    play(SoundToPlay)


    However, the variable type is not the expected one since the following error is brought:

    TypeError: a bytes-like object is required, not '_io.BytesIO'


    I've tried to see if I could someway convert my variable into a bytes-like object but I found nothing so far. Is there anything?

    Continue reading...

Compartilhe esta Página