Play audio on local computer



  • I am trying to create a website where users can input text, and the text will be read out on my local machine.

    To achieve this, I have used a simple Flask app with Google cloud text to speech, and am deploying it through Google App Engine. The app is functional, and no errors appear in the app engine error log, however, the audio will not play.

    I am wondering how to send audio from the app to be played by my local computer. Is it possible? Or do I need to use a completely different method? I'm very new to all these tools.

    Here is my main.py:

    from flask import Flask, render_template, send_file, request
        from gtts import gTTS
        from google.cloud import texttospeech
        import os
        import tempfile
        from playsound import playsound
    
        app = Flask(__name__)
    
    
        @app.route("/")
        def homepage():
            return render_template("page.html", title="HOME PAGE")
    
        @app.route('/', methods=['GET','POST'])
        def index():
            if request.method == "POST":
                text = request.form['text']
    
                os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="credentials.json"
    
                client = texttospeech.TextToSpeechClient()
                input_text = texttospeech.types.SynthesisInput(text=text)
                voice = texttospeech.types.VoiceSelectionParams(
                    language_code='en-US',
                    name='en-US-Standard-C',
                    ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
    
                audio_config = texttospeech.types.AudioConfig(
                    audio_encoding=texttospeech.enums.AudioEncoding.MP3)
    
                response = client.synthesize_speech(input_text, voice, audio_config)
    
                # The response's audio_content is binary.
                with open('/tmp/output.mp3', 'wb') as out:
                    out.write(response.audio_content)
                    os.system("start output.mp3")
                thankyou = "thanks"
                return thankyou
    
        if __name__ == "__main__":
            app.run(debug=True)
    
    

    Here is page.html:

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title>Talk to Wednesday</title>
      </head>
      <body>
        <h1> Talk to Me </h1>
        <form method="POST">
          <input name="text">
          <input type="submit">
        </form>
      </body>
    </html>
    


  • Is this the forum you meant to ask this on?

    But two things:

    you're trying to use the output audio before you've closed the write handle so maybe the file isn't fully written yet

    you run "start output.mp3" but will that find "/tmp/output.mp3" which is what you wrote? I suspect the working directory is not currently /tmp. So try "start /tmp/output.mp3" as well?

    Or find a Python library which plays MP3s and use that.

    And definitely check that the output.mp3 does get written and can be played with a normal MP3 player.


Log in to reply