question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get audio output directly

See original GitHub issue

Hello,

i have following code:

text = "Hello, my name is Josef."
targetLanguage = 'de'
tts = gTTS(text, targetLanguage)  
tts.save("synthesized.mp3")

Since i play the output directly after this code, is there a way to get the audio output directly as a variable? So i do not have to save and open the mp3 file again.

Thanks! Josef

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:11
  • Comments:53 (3 by maintainers)

github_iconTop GitHub Comments

40reactions
hpollonicommented, Nov 10, 2019

This worked for me:

from gtts import gTTS
import pygame
from io import BytesIO

pygame.init()

def say(text):
    tts = gTTS(text=text, lang='en')
    fp = BytesIO()
    tts.write_to_fp(fp)
    fp.seek(0)
    pygame.mixer.init()
    pygame.mixer.music.load(fp)
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        pygame.time.Clock().tick(10)
38reactions
pndurettecommented, Feb 23, 2016

Yes it’s possible. You need a module that will play from a file-like/file object. Instead of doing tts.save() you can use tts.write_to_fp() to write to a file-like object that you can then reuse to play. As a really simple example:

from gtts import gTTS
from tempfile import TemporaryFile
tts = gTTS(text='Hello', lang='en')
f = TemporaryFile()
tts.write_to_fp(f)
# Play f
f.close()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Connect Speakers to Your TV in 5 Easy Steps
Connecting Your TV to a Speaker System · Step 1: Choose the Type of Speaker · Step 2: Check Your TV's Audio Output...
Read more >
How to Connect Your TV to an External Audio System - Lifewire
RCA/Aux: Run RCA cables from TV audio output to input of the speaker system. Set the Audio output from the TV settings.
Read more >
How to connect speakers to your TV - SoundGuys
Step 1: Look at the back of your TV · Step 2: Identify your audio output options · Step 3: Reconnect your TV...
Read more >
How To Connect External Speakers To A TV Without Audio ...
If you have an optical output, you can simply connect directly to most speakers with optical input or use an optical to RCA...
Read more >
How to output microphone directly to speakers? | FAQ - Gigabyte
How to output microphone directly to speakers? · Go to [Control Panel] > [Hardware and Sound] > [Sound]. · Find [Recording] tab >...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found