python-pyaudio:OSError:[Errno输入溢出] -9981

时间:2017-11-02 09:13:44

标签: python audio raspberry-pi recording pyaudio

我正在使用树莓派来录制音频:按按钮开始录制,再按一次按钮停止录制。但得到错误:

Traceback (most recent call last):
  File "/home/pi/november03 fullscreenwithoutdots.py", line 225, in <module>
    record_audio(is_recording, button_pressed)
   File "/home/pi/november03 fullscreenwithoutdots.py", line 154, in record_audio
    data = stream.read(CHUNK)
   File "/usr/lib/python3/dist-packages/pyaudio.py", line 605, in read
    return pa.read_stream(self._stream, num_frames)
OSError: [Errno Input overflowed] -9981

我搜索了在线解决方案,但在我的情况下它们都没有效果,我从官方文档中成功运行了示例。谁能帮我?非常感谢你。以下是我的代码:

recording_started = False
CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS=5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    frames_per_buffer=CHUNK
    )

frames = []

def record_audio(is_recording, button_pressed):
    CHUNK = 8192
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    RECORD_SECONDS=5
    WAVE_OUTPUT_FILENAME = "output.wav"


    if is_recording:
        print (" Recoding")
        while True:
            if joystick.get_button(0) != 0 and is_recording and not  button_pressed:
                print('stop inside')
                button_pressed = True
                break

            else:
                if joystick.get_button(0)!= 0 and button_pressed:
                    button_pressed = False
                data = stream.read(CHUNK)
                frames.append(data)
                print('recorded')
#detect button press
if joystick.get_numbuttons() >= 1 and joystick.get_button( 0 ) == 0 and  button_pressed:
    button_pressed = False
    is_recording = not is_recording

    #start recording
    if is_recording:
        dotting = dotting + 1
        #change background
        background = pygame.image.load(background_image2).convert()
        #start timer
        stopFlag = Event()
        timer = MyTimer(stopFlag,time.clock())
        #timer.start()
        text2 = font.render('.', True, BLACK)

        screen.blit(background, (0,0))

        stopFlag.set()

        #record, remaining here
        record_audio(is_recording, button_pressed)


 #stop recording
    else:
        #stop recording, save record
        stream.stop_stream()
        stream.close()
        p.terminate()

        print('stop outside')
        wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b''.join(frames))
        wf.close()
        #back to previous question
        timer.do_run = False
        backToPreviousQuestion()
        background = pygame.image.load(background_image).convert()

0 个答案:

没有答案