pyaudio报错
系统:windows代码:
##coding=utf-8
import pyaudio, wave
from tkinter import *
import threading
import tkinter.filedialog, tkinter.messagebox
class RecordVoice(threading.Thread):
def __init__(self):
self.CHUNK = 1024
self.FORMAT = pyaudio.paInt16
print(pyaudio.paInt16)
self.CHANNELS = 2
self.RATE = 44100
self.RECORD_SECONDS = 15
self.FLAG = True
self.root = Tk()
self.root.title(u"录音工具")
self.sourcefile = StringVar(self.root)
def openpath(self):
#选择录音文件保存的路径
filename = tkinter.filedialog.asksaveasfilename(title=u"保存音频文件", defaultextension='wav', filetypes=[("wav file", ".wav")])
print("filename:", filename)
self.sourcefile.set(filename)
if self.sourcefile.get() != '':
self.startbutton.config(state="active")
def startRecord(self):
self.startbutton.config(state="disabled")
#启动录音线程
self.FLAG = True#将FLAG恢复默认值
threading.Thread.__init__(self)#每次重新录音都新起一个线程
threading.Thread.start(self)
self.stopbutton.config(state="active")
def run(self):
#开始录音
self.audio = pyaudio.PyAudio()
#捕获当未选择保存位置时的异常
try:
self.wf = wave.open(self.sourcefile.get(), 'wb')
self.wf.setnchannels(self.CHANNELS)
self.wf.setsampwidth(self.audio.get_sample_size(self.FORMAT))
self.wf.setframerate(self.RATE)
self.stream = self.audio.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
print('* start recording *')
while self.FLAG:
self.wf.writeframes(self.stream.read(self.CHUNK))
except FileNotFoundError:
#当未选择保存位置时,弹出提示框,并将录音按钮置灰
tkinter.messagebox.showerror("错误", "请选择保存位置")
self.stopbutton.config(state="disabled")
self.startbutton.config(state="disabled")
def stopRecord(self):
# 停止录音
print('* done recording *')
self.FLAG = False #需要另起一个线程,在录音过程中监控该flag,一旦为false则停止录音
self.stream.stop_stream()
self.stream.close()
self.audio.terminate()
self.wf.close()
self.stopbutton.config(state="disabled")
self.startbutton.config(state="active")
def recordUI(self):
self.frame = Frame(self.root, padx=10, pady=10)
self.frame.pack()
self.frame2 = Frame(self.root, padx=10, pady=10)
self.frame2.pack()
Label(self.frame, text="保存位置").grid(row=0, column=0,padx=5)
Entry(self.frame, textvariable=self.sourcefile, state="readonly").grid(row=0, column=1,padx=5)
Button(self.frame, text="打开", command=self.openpath).grid(row=0, column=2,padx=5)
self.startbutton = Button(self.frame2, text="开始录音", command=self.startRecord, state="disabled")
self.startbutton.grid(row=0, column=0,padx=10)
self.stopbutton = Button(self.frame2, text="停止录音", command=self.stopRecord, state="disabled")
self.stopbutton.grid(row=0, column=1,padx=10)
self.root.mainloop()
if __name__ == "__main__":
p = RecordVoice()
p.recordUI()
报错:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\x4\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "d:\runc\fishc.py", line 47, in run
self.stream = self.audio.open(format=self.FORMAT,
File "C:\Users\x4\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "C:\Users\x4\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: Invalid number of channels 没人?
页:
[1]