I want to make a program with a recording and a stop button, and a label on top to show if it is still recording or done recording. I took the basic recording-code-structure from another question, there probably could be a mistake in how I rewrote it.
Now to my problem: the widow is opening and it looks like how I want it to look, but as soon as I click anything aftrer the 'record' button the program hangs itself (with the label changed to 'recording...'). One time I also waited about 10 minutes to see if I wasn't just too impatient the times before (that was not the problem) it goes until Windows says 'Python is not responding' because I clicked the exit button / too many times on the window.
before clicking -> recording, stop button clicked
working with: Python 3.10.1 & VSCode 1.63.2
Any help will really be appreciated!
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import pyaudio as pa
import wave
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Rec Audio")
self.stoped = False
vbox = QVBoxLayout()
self.labelRec = QLabel('')
self.labelRec.setFixedSize(130, 15)
hbox = QHBoxLayout()
self.recbtn = QPushButton('▶ record')
self.recbtn.setFixedSize(90, 30)
self.recbtn.clicked.connect(self.recAudio)
self.stopbtn = QPushButton('▪')
self.stopbtn.setFixedSize(40, 30)
self.stopbtn.clicked.connect(self.stopRec)
hbox.addWidget(self.recbtn)
hbox.addWidget(self.stopbtn)
vbox.addWidget(self.labelRec)
vbox.addLayout(hbox)
self.setLayout(vbox)
def recAudio(self):
audio = pa.PyAudio()
frames = []
stream = audio.open(format=pa.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
self.stoped = False
self.labelRec.setText('◉ recording...')
self.repaint()
while self.stoped == False:
data = stream.read(1024)
frames.append(data)
stream.close()
self.labelRec.setText('recording stopped')
wf = wave.open('test_recording.wav', 'wb')
wf.setnchannels(1)
wf.setsampwidth(audio.get_sample_size(pa.paInt16))
wf.setframerate(44100)
wf.writeframes(b''.join(frames))
wf.close()
def stopRec(self):
self.stoped = True
app = QApplication([])
win = Window()
win.show()
sys.exit(app.exec())