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

    from flask import Flask, render_template, send_file, request
        from gtts import gTTS
        from import texttospeech
        import os
        import tempfile
        from playsound import playsound
        app = Flask(__name__)
        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']
                client = texttospeech.TextToSpeechClient()
                input_text = texttospeech.types.SynthesisInput(text=text)
                voice = texttospeech.types.VoiceSelectionParams(
                audio_config = texttospeech.types.AudioConfig(
                response = client.synthesize_speech(input_text, voice, audio_config)
                # The response's audio_content is binary.
                with open('/tmp/output.mp3', 'wb') as out:
                    os.system("start output.mp3")
                thankyou = "thanks"
                return thankyou
        if __name__ == "__main__":

    Here is page.html:

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

  • 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