PyAudio通过网络崩溃
你好,我正面临PyAudio的问题,我无法解决。 当我通过网络使用它时(过了一段时间)它崩溃了。 我得到的错误是
Exception in thread Thread-1: Traceback (most recent call last): File "C:Python27libthreading.py", line 552, in __bootstrap_inner self.run() File "C:Python27libthreading.py", line 505, in run self.__target(*self.__args, **self.__kwargs) File "C:UsersmaboroshiDesktopmyChatchat.py", line 71, in server frames_per_buffer = chunk) File "C:Python27libsite-packagespyaudio.py", line 714, in open stream = Stream(self, *args, **kwargs) File "C:Python27libsite-packagespyaudio.py", line 396, in __init__ self._stream = pa.open(**arguments) IOError: [Errno Device unavailable] -9985
我的代码在下面(大部分是:--P
这用于解密数据和管理连接
def decrypt_my_message(msg): iv = "1234567812345678" key = your_friends_key if len(key) not in (16, 24, 32): raise ValueError("Key must be 16, 24, or 32 bytes") if (len(msg) % 16) != 0: raise ValueError("Message must be a multiple of 16 bytes") if len(iv) != 16: raise ValueError("IV must be 16 bytes") cipher = AES.new(key, AES.MODE_CBC, iv) plaintext = cipher.decrypt(msg) return plaintext ### Server function ### def server(): HOST = '' PORT = 9001 ### Initialize socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind((HOST, PORT)) server_socket.listen(5) ### Start recieve loop read_list = [server_socket] while True: readable, writable, errored = select.select(read_list, [], []) for s in readable: if s is server_socket: conn, addr = s.accept() read_list.append(conn) print "Connection from ", addr else: msg = conn.recv(2024) if msg: cmd, msg = ord(msg[0]),msg[1:] if cmd == CMD_MSG: listb1.insert(END, decrypt_my_message(msg.strip()) + "n") listb1.yview(END) elif cmd == CMD_AUDIO: #d = speex.Decoder() #d.initialize(speex.SPEEX_MODEID_WB) p = pyaudio.PyAudio() stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, output = True, frames_per_buffer = chunk) stream.write(decrypt_my_message(msg), chunk) #Write the data back out to the speakers else: s.close() read_list.remove(s)
这是用于连接和发送加密音频
def encrypt_my_audio_message(msg): key = your_key iv = '1234567812345678' aes = AES.new(key, AES.MODE_CBC, iv) encoder = PKCS7Encoder() pad_text = encoder.encode(msg) msg = aes.encrypt(pad_text) return msg def connectToServer(): CLIENT_PORT = 9001 CLIENT_HOST = str(entryhost.get()) global s try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((CLIENT_HOST, CLIENT_PORT)) print "Connectedn" except: print "Could not connect" ### Client Function ### def client(cmd, msg): try: s.send(cmd + msg) except: print "You are not connected" def sendAudio(): p = pyaudio.PyAudio() stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, output = True, frames_per_buffer = chunk) data = stream.read(chunk) return encrypt_my_audio_message(data) ## stream.stop_stream() ## stream.close() ## p.terminate() def keypress(event): if event.keysym == 'Escape': root.destroy() #x = event.char if event.keysym == 'Control_L': #print("Sending Data...") client(chr(CMD_AUDIO), sendAudio()) #print("Data Sent!")
任何想法,为什么发生这将是最有帮助的
跳到我身上的事情是,你正在混合一个可以阻止select的线程,而且这个循环看起来有点奇怪。 你有很多东西在两个方向上发生 - 试着把注意力集中在让数据包流入和流出像齿轮啮合在一起,或者确保没有办法让某人在空闲或充满时,预计不会。
你可能会从以下方面找到一些启发
我的猜测是,你遇到了问题,因为你正在创建多个PyAudio对象并打开多个流而没有关闭任何它们。
您应该创建一个PyAudio对象和一个流,并重新使用这些对象。
链接地址: http://www.djcxy.com/p/56433.html