這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用python實(shí)現(xiàn)錄音功能并可隨時(shí)停止錄音,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
站在用戶的角度思考問題,與客戶深入溝通,找到雨山網(wǎng)站設(shè)計(jì)與雨山網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋雨山地區(qū)。# -*- coding: utf-8 -*- import pyaudio import time import threading import wave class Recorder(): def __init__(self, chunk=1024, channels=1, rate=64000): self.CHUNK = chunk self.FORMAT = pyaudio.paInt16 self.CHANNELS = channels self.RATE = rate self._running = True self._frames = [] def start(self): threading._start_new_thread(self.__recording, ()) def __recording(self): self._running = True self._frames = [] p = pyaudio.PyAudio() stream = p.open(format=self.FORMAT, channels=self.CHANNELS, rate=self.RATE, input=True, frames_per_buffer=self.CHUNK) while(self._running): data = stream.read(self.CHUNK) self._frames.append(data) stream.stop_stream() stream.close() p.terminate() def stop(self): self._running = False def save(self, filename): p = pyaudio.PyAudio() if not filename.endswith(".wav"): filename = filename + ".wav" wf = wave.open(filename, 'wb') wf.setnchannels(self.CHANNELS) wf.setsampwidth(p.get_sample_size(self.FORMAT)) wf.setframerate(self.RATE) wf.writeframes(b''.join(self._frames)) wf.close() print("Saved") if __name__ == "__main__": for i in range(1,4): a = int(input('請(qǐng)輸入相應(yīng)數(shù)字開始:')) if a == 1: rec = Recorder() begin = time.time() print("Start recording") rec.start() b = int(input('請(qǐng)輸入相應(yīng)數(shù)字停止:')) if b == 2: print("Stop recording") rec.stop() fina = time.time() t = fina - begin print('錄音時(shí)間為%ds'%t) rec.save("1_%d.wav"%i)