英文:
Intercom (not input- output Stream)
问题
我有一个问题:如果我反复按下"接收音频"和"开始"按钮,我会收到以下错误消息:
发生错误:[Errno Not input stream] -9975
发生错误:[Errno Not output stream] -9974
我想要同时向服务器端发送和接收音频,但我无法做到。
这是我的代码:
def send_audio(self):
p = pyaudio.PyAudio()
while self.is_running:
try:
self.stream = p.open(format=pyaudio.paInt16,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
while self.is_running:
data = self.stream.read(self.CHUNK)
audio_data = np.frombuffer(data, np.int16)
self.server_socket.sendall(audio_data)
if not self.is_running:
break
self.stream.stop_stream()
self.stream.close()
p.terminate()
except Exception as e:
# 处理异常(例如,记录错误,重试等)
print("发生错误:", str(e))
def receive_audio(self):
p = pyaudio.PyAudio()
while self.is_running_recv:
try:
self.stream = p.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
output=True)
while self.is_running_recv:
data = self.server_socket.recv(self.CHUNK)
if not data:
break
if self.event.is_set():
self.stream.write(data)
self.stream.stop_stream()
self.stream.close()
self.server_socket.close()
p.terminate()
except Exception as e:
# 处理异常(例如,记录错误,重试等)
print("发生错误:", str(e))
希望这可以帮助你解决问题。
英文:
I have a problem: if I repeatedly press the "receive-audio" and "Start" buttons, I get the following error:
An error occurred: [Errno Not input stream] -9975
An error occurred: [Errno Not output stream] -9974
I want to both send and receive audio to the server side, but I can't.
here is my code:
def send_audio(self):
p = pyaudio.PyAudio()
while self.is_running:
try:
self.stream = p.open(format=pyaudio.paInt16,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK)
while self.is_running:
data = self.stream.read(self.CHUNK)
audio_data = np.frombuffer(data, np.int16)
self.server_socket.sendall(audio_data)
if not self.is_running:
break
self.stream.stop_stream()
self.stream.close()
p.terminate()
except Exception as e:
# Handle the exception (e.g., log the error, retry, etc.)
print("An error occurred:", str(e))
def receive_audio(self):
p = pyaudio.PyAudio()
while self.is_running_recv:
try:
self.stream = p.open(format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
output=True)
while self.is_running_recv:
data = self.server_socket.recv(self.CHUNK)
if not data:
break
if self.event.is_set():
self.stream.write(data)
self.stream.stop_stream()
self.stream.close()
self.server_socket.close()
p.terminate()
except Exception as e:
# Handle the exception (e.g., log the error, retry, etc.)
print("An error occurred:", str(e))
答案1
得分: 0
我解决了这个问题。
我们应该写stream而不是self.stream
stream.stop_stream()
stream.close()
p.terminate()
英文:
i solved this problem.
We should write stream instead of self.stream
stream.stop_stream()
stream.close()
p.terminate()
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论