Jarvis音频朗读/保存异常、旧音频删除及print转音频问题咨询
Hey there, let’s break down each of your problems and fix them with actionable code tweaks and explanations:
1. Fix: Can’t Fetch Audio Format Commands + No Subsequent Audio Playback
If you’re using playsound for audio playback, it often locks up after the first play because it doesn’t properly release system resources. Switching to pygame.mixer is far more reliable for handling multiple audio files in sequence. Also, always explicitly specify valid audio formats (like .wav or .mp3) when loading files.
Example code snippet:
import pygame import os # Initialize pygame mixer once at the start of your script pygame.mixer.init() def play_audio(file_path): if not os.path.exists(file_path): print(f"Error: Audio file {file_path} not found!") return pygame.mixer.music.load(file_path) pygame.mixer.music.play() # Wait for playback to finish before moving to the next task while pygame.mixer.music.get_busy(): continue
2. Fix: Jarvis Only Reads the First Command (No Subsequent Audio/Save)
This usually happens if you’re mishandling the text-to-speech (TTS) engine lifecycle. For offline TTS with pyttsx3, keep the engine instance alive instead of reinitializing it every time. For online TTS with gTTS, avoid overwriting the same audio file for each command—create unique filenames instead.
For pyttsx3 (offline TTS):
import pyttsx3 # Initialize the engine once (don't reinitialize in each speak call!) engine = pyttsx3.init() def speak(text): engine.say(text) engine.runAndWait() # Ensures the text is fully spoken before proceeding # Test multiple commands speak("First instruction") speak("Second instruction") # This will now work as expected!
For gTTS (online TTS):
from gtts import gTTS import playsound def speak_and_save(text, command_id): # Create a unique filename for each command to avoid overwrites file_name = f"command_{command_id}.mp3" tts = gTTS(text=text, lang='en') tts.save(file_name) playsound.playsound(file_name) return file_name # Test multiple commands speak_and_save("First command", 1) speak_and_save("Second command", 2) # Each command gets its own audio file
3. Fix: Delete Old Audio Files After Processing
Use Python’s os module to target and delete old audio files before saving new ones. You can either clean up specific file patterns or track individual files you’ve created.
Example:
import os def cleanup_old_audio(): # Delete all command-specific .mp3 files in the current directory for file in os.listdir(): if file.endswith(".mp3") and file.startswith("command_"): os.remove(file) print(f"Deleted old audio file: {file}") # Usage: Clean up before generating new audio cleanup_old_audio() new_audio_file = speak_and_save("Fresh new command", 3)
4. Fix: Sync Print Statements with Audio Speech
Create a custom function that handles both printing text to the screen and triggering the TTS engine. Replace all your regular print() calls with this function for synchronized output.
Example:
def print_and_speak(text): print(text) speak(text) # Use the speak() function we defined earlier # Test it out print_and_speak("Say something!") print_and_speak("This text is both displayed on screen and spoken aloud.")
内容的提问来源于stack exchange,提问作者Ajay Soni

