Intercom(不是输入输出流)

huangapple go评论69阅读模式
英文:

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.

enter image description here

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>



huangapple
  • 本文由 发表于 2023年6月26日 18:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定