How to save audio file in a folder and play it from there using gtts

42 Views Asked by At

I want to integrate my webpage with a backend flask application. The webpage has a 'hear your response' button which will play the audio recording of an mp3 file generated by gtts. But gtts is not allowing me to save the audio file in a folder from where I can play the file when the button is clicked on my webpage. Please help

I tried both running the audio directly and I tried the send_file option which allows the user to directly download the audio file. But I dont want the user to download the audio file I want him to directly to click the button and hear the answer.

Here's the code that is causing it to get downloaded:


    @app.route('/play_speech')
    def play_speech():
        # Retrieve parameters from the URL
        translated_processed_answer = request.args.get('translated_processed_answer', '')
        selected_language = request.args.get('selected_language', '')
    
        # Strip HTML tags from the translated answer
        cleaned_text = strip_html_tags(translated_processed_answer)
    
        # Generate speech audio for the cleaned text
        speech_text = cleaned_text
        language = selected_language if selected_language else 'en'
    
        if speech_text:
            tts = gTTS(text=speech_text, lang=language, slow=False)
            audio_stream = BytesIO()
            tts.write_to_fp(audio_stream)
            audio_stream.seek(0)
    
            return send_file(audio_stream, mimetype='audio/mpeg', as_attachment=True, download_name='speech.mp3')
        else:
            # Handle the case where there is no text to speak
            return "No text to speak"

and I tried this code to save it to a audio folder:

        @app.route('/play_speech')
        def play_speech():
            # Retrieve parameters from the URL
            translated_processed_answer = request.args.get('translated_processed_answer', '')
            selected_language = request.args.get('selected_language', '')
        
            # Strip HTML tags from the translated answer
            cleaned_text = strip_html_tags(translated_processed_answer)
        
            # Generate speech audio for the cleaned text
            speech_text = cleaned_text
            language = selected_language if selected_language else 'en'
        
            if speech_text:
                tts = gTTS(text=speech_text, lang=language, slow=False)
                
                # Save the audio file in the 'audio' folder
                audio_folder = os.path.join(os.path.dirname(__file__), 'audio')
                os.makedirs(audio_folder, exist_ok=True)
                
                audio_file_path = os.path.join(audio_folder, 'speech.mp3')
                tts.save(audio_file_path)
        
                # Return the path to the audio file
                return send_file(audio_file_path, mimetype='audio/mpeg', as_attachment=True, download_name='speech.mp3')
            else:
                # Handle the case where there is no text to speak
                return "No text to speak" 

but that returns the following error :

 127.0.0.1 - - [08/Oct/2023 12:31:57] "GET /audio/speech.mp3 HTTP/1.1" 404 -
0

There are 0 best solutions below