Skip to main content
 首页 » 编程设计

python之停止 kivy 视频

2025年02月15日6shihaiming

我想在点击事件时停止这个 kivy 视频(默认播放)。我在树莓派上运行这个。这是我的 kv 和 python 代码。

<VideoScreen>: 
name: 'Video' 
BoxLayout: 
    Video: 
        id: 'video1' 
        source: './media/Sequence_#1.mp4' 
        state: root.current_video_state 
        volume: 1 
        options: {'eos': 'loop'} 
        allow_stretch: True 

视频循环播放,点击它切换到新屏幕“登录”,但视频不会停止并且仍在循环播放(我想在加载新屏幕后停止播放)。还有许多其他屏幕屏幕使用屏幕管理器连接到视频屏幕。假设加载屏幕工作正常。忽略缩进。

class VideoScreen(Screen): 
    current_video_state = StringProperty() 
    def __init__(self, **kwargs): 
        super(VideoScreen,  self).__init__(**kwargs) 
        self.bind(on_touch_down = self.on_stop) 
        self.current_video_state = self.get_set_current_video_state() 
 
    def get_set_current_video_state(self, *args): 
        while(args): 
            print('arg', args[0]) 
            if args[0] == 'pause': 
            return 'pause' 
        return 'play' 
 
    def on_stop(self,  *args): 
        self.state = 'pause' 
        self.get_set_current_video_state('pause') 
        self.parent.current = 'Login' 

请您参考如下方法:

Video: 
    # ... 
    state: root.current_video_state 

这部分将视频小部件的状态绑定(bind)到属性 current_video_state:每次 current_video_state 更改时,视频的状态将以​​相同的方式更改。您应该在(触摸)事件上更改此变量以暂停视频:

def on_stop(self,  *args): 
    self.current_video_state = 'pause'  # this will change video state to 'pause' 
    self.parent.current = 'Login'