You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu环境下PyAudio报OSError -9993错误的解决求助

Fixing OSError: [Errno -9993] Illegal combination of I/O devices in Ubuntu 18.04 Conda Env

Let's tackle this issue head-on. From your error logs and code, the problem stems from conflicting audio device access between your TTS (gTTS/mpg123) and STT (Vosk/PyAudio) modules—plus a potential PulseAudio connection glitch. Here are actionable fixes to try, ordered by likelihood of resolving your problem:

1. Ensure TTS completes playback before starting STT

Your current os.system('mpg123 -q audio.mp3') doesn't wait for the audio to finish playing before moving on. This leaves the audio device occupied when your STT module tries to access it for recording. Replace os.system with subprocess.run to force your code to wait for playback to end:

from gtts import gTTS
import subprocess
import os

def talkToMe(mytext):
    print(mytext)
    text_to_speech = gTTS(text=mytext, lang='en-uk')
    audio_file = 'audio.mp3'
    text_to_speech.save(audio_file)
    # Wait for mpg123 to finish playing before proceeding
    subprocess.run(['mpg123', '-q', audio_file], check=True)
    # Clean up the temporary audio file
    os.remove(audio_file)

2. Explicitly specify your input audio device for PyAudio

Sometimes PyAudio picks the wrong input device by default, leading to conflicts. First, list all available audio devices to find your microphone's index:

import pyaudio

p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
    info = p.get_device_info_by_index(i)
    print(f"Device {i}: {info['name']} | Max Input Channels: {info['maxInputChannels']}")
p.terminate()

Look for the device with maxInputChannels > 0 that corresponds to your microphone. Then update your STT code to use this index, and add proper cleanup to avoid resource leaks:

import pyaudio
from vosk import Model, KaldiRecognizer

def myCommand():
    p = pyaudio.PyAudio()
    try:
        # Replace 2 with your microphone's device index
        stream = p.open(format=pyaudio.paInt16, 
                        channels=1, 
                        rate=16000, 
                        input=True, 
                        frames_per_buffer=8000,
                        input_device_index=2)
        stream.start_stream()
        model = Model("model-en")
        rec = KaldiRecognizer(model, 16000)
        while True:
            data = stream.read(2000)
            if len(data) == 0:
                break
            if rec.AcceptWaveform(data):
                # More reliable way to extract text from Vosk's JSON output
                myResult = rec.Result()
                command = myResult.split('"text": "')[1].split('"')[0]
                return command
    finally:
        # Always clean up audio resources to avoid leaks
        stream.stop_stream()
        stream.close()
        p.terminate()

3. Fix PulseAudio connection issues

Your logs show PulseAudio: Unable to connect: Connection terminated, which suggests a problem with the PulseAudio daemon. Restart it to resolve this:

pulseaudio -k && pulseaudio --start

If this works, you can also try running your Python script after setting this environment variable to ensure Conda uses the system's ALSA configuration:

export ALSA_CONFIG_PATH=/usr/share/alsa/alsa.conf
python your_script_name.py

4. Reinstall PyAudio for better system compatibility

Conda's pre-built PyAudio package might not play nicely with Ubuntu's native audio libraries. Uninstall the Conda version and install PyAudio using system pip (you'll need system dependencies first):

# Uninstall Conda's PyAudio
conda uninstall pyaudio -y

# Install system dependencies for PyAudio
sudo apt-get install portaudio19-dev

# Install PyAudio via pip in your Conda environment
pip install pyaudio

Start with fix #1, as that's the most common cause of this error in TTS/STT setups, then work through the other steps if needed.

内容的提问来源于stack exchange,提问作者Micheal Bee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:32:40