python3.7不支持pyaudio pip在线安装
whl下载地址:https://github.com/intxcc/pyaudio_portaudio/releases
下载后使用pip离线安装
pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl
# pyrec.py 文件内容 import pyaudio import wave CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 RECORD_SECONDS = 3 def rec(file_name): p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("开始录音,请说话......") frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) print("录音结束,请闭嘴!") stream.stop_stream() stream.close() p.terminate() wf = wave.open(file_name, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() if __name__ == '__main__': rec("audio/xinyuliu.wav")