python录音

import tkinter as tk
import pyaudio
import wave

from idna import uts46_remap

# 录音参数设置
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 8000
WAVE_OUTPUT_FILENAME = "output.wav"

first_click = True






frames = []

# 录制音频数据
def luyin():
    # 初始化 PyAudio 对象
    p = pyaudio.PyAudio()
    # 打开音频流
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)
    if first_click:
        data = stream.read(CHUNK)
        frames.append(data)
        root.after_idle(luyin)

def ting():
    global first_click
    first_click = False
    stream.stop_stream()
    # 关闭音频流
    stream.close()
    # 终止 PyAudio 对象
    p.terminate()
    # 保存录制的音频为 WAV 文件
    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()



# 创建主窗口
root = tk.Tk()
root.title("录音")
button1 = tk.Button(root, text="录音",width=6,height=1,command=luyin)
button1.place(x=10, y=10)
button2 =tk.Button(root,text="停止",width=6,height=1,command=ting)
button2.place(x=100,y=10)
text_box=tk.Text(root,height=10,width=30)
text_box.place(x=50,y=100)
root.geometry("400x300+100+100")

#button.pack()
root.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

adfass

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值