Windows环境Python3.6.3开发Jarvis AI无输出问题求助
Let's break down why your talkToME function isn't producing any output or audio—even without throwing errors. Here are the most likely issues and fixes tailored to your setup:
1. You never called the function!
Looking at your code, you’ve defined the talkToME function but never actually executed it. Functions only run when you explicitly call them. Add this line at the end of your script to trigger the behavior you expect:
talkToME("I am fine")
Without this, your code just defines the function and exits silently—no wonder you don’t see or hear anything.
2. mpg123 isn’t available on Windows
The os.system('mpg123 audio.mp3') command relies on mpg123, a command-line audio player that doesn’t come pre-installed on Windows. Even if you’ve installed it, if it’s not added to your system PATH, Windows won’t locate it.
A simpler, Windows-native fix is to use os.startfile(), which opens the audio file with your default media player:
os.startfile('audio.mp3')
Replace the mpg123 line with this, and you won’t need extra software to play the audio.
3. File permission issues when saving the audio
Your script might not have permission to save audio.mp3 in its current working directory. Try using an absolute path to a folder you know you can write to (like your Documents folder):
# Replace "YourName" with your actual Windows username audio_path = 'C:/Users/YourName/Documents/jarvis_audio.mp3' tts.save(audio_path) os.startfile(audio_path)
Check if the file appears in that folder—if not, double-check the folder’s write permissions.
4. Verify the audio file is being created
Before troubleshooting playback, confirm that audio.mp3 is actually being generated. Look in the folder where you run your script for the file. If it’s missing, the gTTS.save() step might be failing silently (older Python 3.6 packages can sometimes behave this way). Try upgrading gTTS to a version compatible with Python 3.6:
pip install --upgrade gTTS
Working Modified Example
Here’s a revised version of your code that addresses all these common issues:
from gtts import gTTS import os import speech_recognition as sr import webbrowser import time def talkToME(audioString): print(audioString) tts = gTTS(text=audioString, lang='en') # Use a writable absolute path audio_path = 'C:/Users/YourName/Documents/jarvis_response.mp3' tts.save(audio_path) # Windows-native audio playback os.startfile(audio_path) # Trigger the function with your expected message talkToME("I am fine")
内容的提问来源于stack exchange,提问作者Brian

