I am trying to stop audio from playing when I say stop, but it does not seem to work. The code I currently have is very rudimentary, and is as follows:
import sys
import threading
import tkinter as tk
import pydub
import speech_recognition
import pyttsx3 as tts
import time
import os
from pydub import AudioSegment
from pydub.playback import play
def __innit__(self):
...
self.current_audio = None
self.stop_audio = False
...
if text == "stop":
self.speaker.say("Stopping current command. Returning to listening mode.")
self.speaker.runAndWait()
self.command_event.clear()
self.current_audio.close()
elif "play" in text:
audio_folder = "music"
audio_files = [f for f in os.listdir(audio_folder) if f.endswith(".mp3")]
if audio_files:
if "loop" in text:
while self.command_event.is_set():
for audio_file in audio_files:
audio_file_path = os.path.join(audio_folder, audio_file)
audio = AudioSegment.from_file(audio_file_path)
self.current_audio = audio
play(self.current_audio)
else:
audio_file = AudioSegment.from_file(os.path.join(audio_folder, audio_files[0]))
self.current_audio = audio_file
play(self.current_audio)
else:
self.speaker.say("Sorry, I couldn't find any audio files in the music folder.")
self.speaker.runAndWait()
else:
if text is not None:
response = self.assistant.request(text)
if response is not None:
self.speaker.say(response)
self.speaker.runAndWait()
How would I go about stopping the audio? Should I define .close()
? Pyaudio has a .stop()
in pydub.playback
, but when I try to use that in this program, it doesn't work. Pyaudio had not been working on my computer and I have tried everything to get it to work, but I just cant seem to. Playback.stop()
does not work.