Python 3和口袋狮身人面像

2024-05-16 07:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图为我的祖父创建一个简单的音乐播放器谁有麻烦的按钮编程一个音频控制的音乐播放器。我用的是树莓皮3与python3和口袋狮身人面像。因为Pocket Sphinx不需要互联网,所以我会使用它,因为我的祖父不能上网。在

我的问题是如何获取“said”的值,例如:“playbutton”并让它播放wave file“Button”?在

以下是我构建基本程序所需的:

import speech_recognition as sr
import pygame
from pygame import mixer
mixer.init()

r = sr.Recognizer()
m = sr.Microphone()

Button = pygame.mixer.Sound('/home/pi/Downloads/button8.wav')

try:
    print("A moment of silence, please...")
    with m as source: r.adjust_for_ambient_noise(source)
    print("Set minimum energy threshold to {}".format(r.energy_threshold))
    while True:
        print("Say something!")
        with m as source: audio = r.listen(source)
        print("Got it! Now to recognize it...")
        try:
            # recognize speech using Sphinx
            value = r.recognize_sphinx(audio)
            print("You said {}".format(value)) #uses unicode for strings and this is where I am stuck
            pygame.mixer.Sound.play(Button)
            pygame.mixer.music.stop()
        except sr.UnknownValueError:
            print("Oops! Didn't catch that")
        except sr.RequestError as e:
            print("Uh oh! Couldn't request results; {0}".format(e))
except KeyboardInterrupt:
    pass

非常感谢您能提供的任何帮助。请客气点,因为我是初学者。在


Tags: importformatsource音乐assphinxbutton播放器
1条回答
网友
1楼 · 发布于 2024-05-16 07:23:17

尝试将其与“播放按钮”进行比较:

# recognize speech using Sphinx
value = r.recognize_sphinx(audio)
print("You said {}".format(value))

if value.lower() == 'play button':
    pygame.mixer.Sound.play(Button)
    pygame.mixer.music.stop()

相关问题 更多 >