鱼C论坛

 找回密码
 立即注册
查看: 1511|回复: 1

pyaudio报错

[复制链接]
发表于 2020-9-5 12:02:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
系统:windows
代码:
  1. ##coding=utf-8
  2. import pyaudio, wave
  3. from tkinter import *
  4. import threading
  5. import tkinter.filedialog, tkinter.messagebox

  6. class RecordVoice(threading.Thread):
  7.     def __init__(self):
  8.         self.CHUNK = 1024
  9.         self.FORMAT = pyaudio.paInt16
  10.         print(pyaudio.paInt16)
  11.         self.CHANNELS = 2
  12.         self.RATE = 44100
  13.         self.RECORD_SECONDS = 15
  14.         self.FLAG = True

  15.         self.root = Tk()
  16.         self.root.title(u"录音工具")
  17.         self.sourcefile = StringVar(self.root)

  18.     def openpath(self):
  19.         #选择录音文件保存的路径
  20.         filename = tkinter.filedialog.asksaveasfilename(title=u"保存音频文件", defaultextension='wav', filetypes=[("wav file", ".wav")])
  21.         print("filename:", filename)
  22.         self.sourcefile.set(filename)
  23.         if self.sourcefile.get() != '':
  24.             self.startbutton.config(state="active")

  25.     def startRecord(self):
  26.         self.startbutton.config(state="disabled")

  27.         #启动录音线程
  28.         self.FLAG = True  #将FLAG恢复默认值
  29.         threading.Thread.__init__(self)  #每次重新录音都新起一个线程
  30.         threading.Thread.start(self)

  31.         self.stopbutton.config(state="active")

  32.     def run(self):
  33.         #开始录音
  34.         self.audio = pyaudio.PyAudio()
  35.         #捕获当未选择保存位置时的异常
  36.         try:
  37.             self.wf = wave.open(self.sourcefile.get(), 'wb')
  38.             self.wf.setnchannels(self.CHANNELS)
  39.             self.wf.setsampwidth(self.audio.get_sample_size(self.FORMAT))
  40.             self.wf.setframerate(self.RATE)
  41.             self.stream = self.audio.open(format=self.FORMAT,
  42.                             channels=self.CHANNELS,
  43.                             rate=self.RATE,
  44.                             input=True,
  45.                             frames_per_buffer=self.CHUNK)
  46.             print('* start recording *')
  47.             while self.FLAG:
  48.                 self.wf.writeframes(self.stream.read(self.CHUNK))
  49.         except FileNotFoundError:
  50.             #当未选择保存位置时,弹出提示框,并将录音按钮置灰
  51.             tkinter.messagebox.showerror("错误", "请选择保存位置")
  52.             self.stopbutton.config(state="disabled")
  53.             self.startbutton.config(state="disabled")

  54.     def stopRecord(self):
  55.         # 停止录音
  56.         print('* done recording *')
  57.         self.FLAG = False   #需要另起一个线程,在录音过程中监控该flag,一旦为false则停止录音

  58.         self.stream.stop_stream()
  59.         self.stream.close()
  60.         self.audio.terminate()
  61.         self.wf.close()

  62.         self.stopbutton.config(state="disabled")
  63.         self.startbutton.config(state="active")

  64.     def recordUI(self):
  65.         self.frame = Frame(self.root, padx=10, pady=10)
  66.         self.frame.pack()

  67.         self.frame2 = Frame(self.root, padx=10, pady=10)
  68.         self.frame2.pack()

  69.         Label(self.frame, text="保存位置").grid(row=0, column=0,padx=5)
  70.         Entry(self.frame, textvariable=self.sourcefile, state="readonly").grid(row=0, column=1,padx=5)
  71.         Button(self.frame, text="打开", command=self.openpath).grid(row=0, column=2,padx=5)

  72.         self.startbutton = Button(self.frame2, text="开始录音", command=self.startRecord, state="disabled")
  73.         self.startbutton.grid(row=0, column=0,padx=10)
  74.         self.stopbutton = Button(self.frame2, text="停止录音", command=self.stopRecord, state="disabled")
  75.         self.stopbutton.grid(row=0, column=1,padx=10)

  76.         self.root.mainloop()


  77. if __name__ == "__main__":
  78.     p = RecordVoice()
  79.     p.recordUI()
复制代码


报错:
  1. Exception in thread Thread-1:
  2. Traceback (most recent call last):
  3.   File "C:\Users\x4\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
  4.     self.run()
  5.   File "d:\runc\fishc.py", line 47, in run
  6.     self.stream = self.audio.open(format=self.FORMAT,
  7.   File "C:\Users\x4\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio.py", line 750, in open
  8.     stream = Stream(self, *args, **kwargs)
  9.   File "C:\Users\x4\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio.py", line 441, in __init__
  10.     self._stream = pa.open(**arguments)
  11. OSError: [Errno -9998] Invalid number of channels
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-5 21:33:30 | 显示全部楼层
没人?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 17:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表