Pic*_*ana 5 python exception tkinter ioerror pyaudio
我正在尝试做一个只有一个按钮的简单GUI应用程序:Record.
您按下按钮,录音开始.松开按钮时,录制停止并保存录制内容.
但是,单击按钮时出现以下错误:
Traceback (most recent call last):
...
data = self.stream.read(self.CHUNK)
File (...), line 608, in read
return pa.read_stream(self._stream, num_frames, exception_on_overflow)
IOError: [Errno -9981] Input overflowed
Exception in Tkinter callback
Run Code Online (Sandbox Code Playgroud)
但是,如果没有按钮和Tkinter(这里给出的代码示例),我没有记录简单音频的问题.
这是代码:
import Tkinter as tk
import pyaudio, wave
class AppRecording:
def __init__(self, root):
self.root = root
self.mouse_pressed = False
recordingButton = tk.Button(root, text = "Record")
recordingButton.pack()
recordingButton.bind("<ButtonPress-1>", self.OnMouseDown)
recordingButton.bind("<ButtonRelease-1>", self.OnMouseUp)
self.CHUNK = 1024
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 2
self.RATE = 44100
self.WAVE_OUTPUT_FILENAME = "output.wav"
self.p = pyaudio.PyAudio()
try: self.stream = self.p.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
except:
raise Exception("There is no connected microphone. Check that you connect to the left hole if you have a PC.")
return None
self.frames = []
def recordFrame(self):
try:
data = self.stream.read(self.CHUNK)
print "after try"
except IOError as ex:
print "inside except"
if ex[1] != pyaudio.paInputOverflowed:
print "before raise"
raise
print "after raise"
data = '\x00' * self.CHUNK # or however you choose to handle it, e.g. return None
self.frames.append(data)
def finishRecording(self):
self.stream.stop_stream()
self.stream.close()
self.p.terminate()
wf = wave.open(self.WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(self.CHANNELS)
wf.setsampwidth(self.p.get_sample_size(self.FORMAT))
wf.setframerate(self.RATE)
wf.writeframes(b''.join(self.frames))
wf.close()
def OnMouseDown(self, event):
self.mouse_pressed = True
self.poll()
def OnMouseUp(self, event):
self.root.after_cancel(self.after_id)
print "Finished recording!"
self.finishRecording()
def poll(self):
if self.mouse_pressed:
self.recordFrame()
self.after_id = self.root.after(1, self.poll)
root=tk.Tk()
app = AppRecording(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我试图改变self.CHUNK和self.RATE.我的iMac的内部麦克风说速率是44100.在某些地方,我读到我应该更改块或速率值,试过两个但没人帮忙.另一个地方告诉我要添加except IOError as ex: (...)
PyAudio版本:0.2.10
pyaudio.get_portaudio_version():1246720
pyaudio.get_portaudio_version_text():PortAudio V19.6.0-devel,修订版396fe4b6699ae929d3a685b3ef8a7e97396139a4
Tkinter.__version__:$ Revision:81008 $
非常感谢你的帮助,谢谢!
哪个 python/tk/portaudio/pyaudio 版本?
我确认您的代码在 Ubuntu 14.04 LTS x64(带有 portaudio19-dev 和 PyAudio-0.2.10 的 Python 2.7)下运行良好(没有问题),所以我认为该问题可能与您的 python、tk、pyaudio 或 portaudio 版本有关...
您确定您的计算机上安装了最新的 portaudio & tk 版本吗?
| 归档时间: |
|
| 查看次数: |
501 次 |
| 最近记录: |