代码之家  ›  专栏  ›  技术社区  ›  avocado123123

只要鼠标点击,kivymd屏幕就会变黑

  •  0
  • avocado123123  · 技术社区  · 3 年前

    我正在编写一个使用客户端-服务器架构的音乐流媒体平台。在客户端,我使用kivymd库来编写GUI。当流媒体播放时,客户端会显示一个屏幕,上面写着“立即流媒体播放”,还有播放和暂停按钮。出于某种原因,当流媒体发生时(工作正常),我们在“立即流媒体”屏幕上,如果用户的鼠标点击屏幕上的任何地方,无论是按钮还是其他地方,屏幕就会变黑。即使屏幕是黑色的,流媒体也不会停止——这首歌听得很好。

    这是客户端流媒体屏幕的类:

    class StreamingScreen(Screen):
    global is_playing
    is_playing = True
    
    streaming_thread = None
    
    def on_enter(self):
        self.streaming_thread = threading.Thread(target=self.play_song())
        # self.play_song()
        self.streaming_thread.start()
        print('started the thread')
    
    def pause(self, *args):
        global is_playing
        print('pause pressed')
        is_playing = False
        # Send a pause command to the server
        client.send('PAUSE'.encode())
    
    def resume(self):
        global is_playing
        print('resume pressed')
        is_playing = True
        # Send a resume command to the server
        client.send('RESUME'.encode())
    
    def play_song(self):
        global is_playing
    
        client.send('GOOD'.encode(FORMAT))
        print('now playing song')
    
        # Stream song
        p = pyaudio.PyAudio()
        stream = p.open(format=p.get_format_from_width(2),
                        channels=2,
                        rate=44100,
                        output=True,
                        frames_per_buffer=CHUNK)
    
        data = b""
        payload_size = struct.calcsize("Q")
    
        while True:
            if is_playing:
                while len(data) < payload_size:
                    packet = client.recv(4 * 1024)  # 4K
                    if not packet:
                        break
                    data += packet
                packed_msg_size = data[:payload_size]
                data = data[payload_size:]
                msg_size = struct.unpack("Q", packed_msg_size)[0]
                while len(data) < msg_size:
                    data += client.recv(4 * 1024)
                frame_data = data[:msg_size]
                data = data[msg_size:]
                frame = pickle.loads(frame_data)
                print('writing to stream')
                stream.write(frame)
            elif not is_playing:
                # Pause the stream
                print('streaming paused')
                time.sleep(0.1)
    

    负责流媒体屏幕的kv文件:

    <StreamingScreen>:
    name: 'streaming'
    
    MDBoxLayout:
        orientation: 'vertical'
        spacing: '20dp'
        padding: '20dp'
    
        MDLabel:
            text: "Now streaming"
            font_style: "H2"
            theme_text_color: "Secondary"
            halign: 'center'
    
        MDBoxLayout:
            size_hint_y: None
            height: self.minimum_height
    
            MDFloatLayout:
                MDIconButton:
                    icon: "arrow-left"
                    theme_text_color: "Custom"
                    text_color: (1, 1, 1, 1)
                    size_hint: None, None
                    size: '72dp', '72dp'
                    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                    radius: [36]
                    md_bg_color: (0.5, 0, 0.5, 1)
                    on_release:
                        root.manager.transition.direction = 'right'
                        app.navigation_draw()
    
            MDFloatLayout:
                MDIconButton:
                    icon: "play"
                    theme_text_color: "Custom"
                    text_color: (1, 1, 1, 1)
                    size_hint: None, None
                    size: '96dp', '96dp'
                    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                    radius: [48]
                    md_bg_color: (0.5, 0, 0.5, 1)
                    on_press:
                        root.play()
    
            MDFloatLayout:
                MDIconButton:
                    icon: "pause"
                    theme_text_color: "Custom"
                    text_color: (1, 1, 1, 1)
                    size_hint: None, None
                    size: '72dp', '72dp'
                    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                    radius: [36]
                    md_bg_color: (0.5, 0, 0.5, 1)
                    on_press:
                        root.pause()
    

    当我按下“暂停”按钮时,甚至不会调用“暂停”功能。有些东西因为鼠标点击而崩溃,我不明白为什么。任何建议都将不胜感激!

    0 回复  |  直到 3 年前