播放视频后如何在kivy中更改当前屏幕

时间:2016-02-07 17:43:40

标签: python kivy

我的应用程序有一个按钮>点击按钮将开始播放视频>视频完成后,我想导航到最终屏幕。

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
import webbrowser
from kivy.uix.videoplayer import VideoPlayer
import time

Builder.load_string('''
<MenuScreen>:
    GridLayout:
        padding: 5
        spacing: 5
        cols: 1
        padding: root.width*0.1
        Button:
            background_normal: ''
            background_color:(0.862, 0.078, 0.235, 0.9)
            text: 'PLAY'
            font_size: '20sp'
            on_press: root.manager.current='vdo'
            on_press: root.val1()
<Vdo>:
    GridLayout:
        cols: 1
        VideoPlayer:
            id: testid
            state: 'stop'
            source: 'http://techslides.com/demos/sample-videos/small.webm'
            fullscreen: True
            image_play: 'black.png'
            image_stop: 'black.png'
            image_pause: 'black.png'
            image_volumehigh: 'black.png'
            image_volumelow: 'black.png'
            image_volumemedium: 'black.png'
            image_volumemuted: 'black.png'
<Final>:
    GridLayout:
        cols: 1
        Label:
            text: 'over'

''')

class MenuScreen(Screen):
    def val1(self):
        print "1 is executed"
        vdo.ids.testid.state='play'
        sm.current='final'

class Vdo(Screen):
    def val2(self):
        print "i am executed"

class Final(Screen):
    def val3(self):
        print "i am executed"

sm = ScreenManager()
menu = MenuScreen(name='menu')
sm.add_widget(menu)
vdo = Vdo(name='vdo')
sm.add_widget(vdo)
final = Final(name='final')
sm.add_widget(final)

class MainApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    MainApp().run()

视频播放完毕后,我的代码无法移动到最终屏幕。我尝试在视频播放后给予时间睡眠,但在此期间视频将在后台播放。

1 个答案:

答案 0 :(得分:1)

我会尝试将一个函数绑定到VideoPlayer状态属性,以便在触发时你将检查position == duration(结束)并移动到下一个屏幕

<Vdo>:
    video: testid #easier to access this way
    VideoPlayer:
        id: testid
        state: 'stop'
        source: 'http://techslides.com/demos/sample-videos/small.webm'
        fullscreen: True
        image_play: 'black.png'
        image_stop: 'black.png'
        image_pause: 'black.png'
        image_volumehigh: 'black.png'
        image_volumelow: 'black.png'
        image_volumemedium: 'black.png'
        image_volumemuted: 'black.png'
你的python代码中的

def state_change(instance, value):
    if vdo.video.duration == vdo.video.position:
        sm.current = 'final'
vdo.video.state.bind(state_change)

我希望这会有所帮助