PyAudio Over Network 崩溃
您好,我遇到了 PyAudio 的问题,无法解决。当我通过网络使用它时(一段时间后)它崩溃了。我得到的错误是
Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner self.run() File "C:\Python27\lib\threading.py", line 505, in run self.__target(*self.__args, **self.__kwargs) File "C:\Users\maboroshi\Desktop\myChat\chat.py", line 71, in server frames_per_buffer = chunk) File "C:\Python27\lib\site-packages\pyaudio.py", line 714, in open stream = Stream(self, *args, **kwargs) File "C:\Python27\lib\site-packages\pyaudio.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 "Connected\n" 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!")
任何想法为什么会发生这种情况将是最有帮助的
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我惊讶的是,您将 阻塞的线程 与select,循环看起来有点奇怪。你有很多事情在两个方向上进行——尝试并专注于使数据包的进出流像齿轮啮合在一起一样工作,或者确保有人在他们’时不会是空的或满的。我们预计两者都不会。
您可能会从以下内容中找到一些灵感:
The thing that jumps out at me is that you're mixing a thread that blocks with select, and the loop looks a bit weird. You've got a lot of stuff going on in both directions - try and focus on making the flow of packets in and out work like gears meshing together, or ensure that there's no way that somebody is going to be empty or full when they're expected to be neither.
You might find some inspiration from:
我的猜测是您遇到了问题,因为您正在创建多个 PyAudio 对象并打开多个流而不关闭其中任何一个。
您应该创建一个 PyAudio 对象和一个流,并重新使用这些对象。
My guess is that you're running into problems because you are creating multiple PyAudio objects and opening multiple streams without closing any of them.
You should create one PyAudio object, and one stream, and re-use those objects.