解决ubunut20.04系统麦克风异常问题

背景

解决ubunut20.04系统麦克风异常问题_第1张图片

 在部分ubunut环境中,我们指定了麦克风的下标,使用指定的麦克风有问题,只能录音一次,第二次就失效了,不过只是部分ubunut系统会遇到这个情况

# 找到以usb_mic_prefix为前缀的麦克风下标
import speech_recognition as sr    
def load_mic_index(usb_mic_prefix):
        for index, name in enumerate(sr.Microphone.list_microphone_names()):
            if usb_mic_prefix in name:
                return index


# 用指定的麦克风作为source
self.source = sr.Microphone(
                sample_rate=16000,
                device_index={上面def获取的下标}
            )

    def setup_mic(self):
        recorder = sr.Recognizer()
        recorder.energy_threshold = self.energy
        # 当声音消失self.pause 秒后调用listen_in_background函数中的回调函数
        recorder.pause_threshold = self.pause
        recorder.dynamic_energy_threshold = self.dynamic_energy
        self.continuous_listening = recorder.listen_in_background(
            self.source,
            self.listen_callback if self.listen_callback is not None else self.record_callback
        )

解决办法

 我们直接把我们想用的麦克风改成默认麦克风

pulseaudio -k && pulseaudio --start

# 获取index 
pactl list short sources

vim /etc/pulse/default.pa 

add: set-default-source 1(上面获取到的index)

总结:

找不到为什么会异常,但是有新的解决方法.....凑合着用

你可能感兴趣的:(linux,运维,服务器)