Scrollview to display different videos in kivy lag

2019-07-27 12:31发布

问题:

I'm a beginner in kivy and python. I've been trying to create a scrollview in a page that displays selected video on the screen. But after a while when i selected 5-6 videos to display even though i delete the video widget it starts to lag. And i'm open to any suggestions to better way to handle this kind of application.

from kivy.config import Config

Config.set('graphics', 'width', '1920')
Config.set('graphics', 'height', '1080')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.properties import StringProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.button import Button
import os


from kivy.uix.video import Video
from kivy.uix.videoplayer import VideoPlayer
from kivy.clock import mainthread
from functools import partial
from kivy.core.window import Window
Window.clearcolor = (0, 0, 0, 0)
isThereVideo=False
picture_path="/home/linux/kivyFiles/kivyLogin/assets"
video_path="/home/linux/kivyFiles/kivyLogin/videoAssets/"
class Image(Image):
    pass

class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass
class Selfie(Screen):
    pass    
class RootWidget(Widget):
    def __init__(self, **kwargs):
            super(RootWidget, self).__init__(**kwargs)
            Clock.schedule_once(self.createMultipleButton)

    @mainthread
    def createMultipleButton(self, dt):
            self.root = Widget()
            size_y=150;
            size_x=150;
            for i in range(1):
            folderList = os.listdir(picture_path)
            if len(folderList)==0:

                time.sleep(1)
                break
            fileList = os.listdir(picture_path)
            print fileList

            for file in fileList:


                x = (picture_path+"/"+file)

                    button = Button(id=str(file),text="" + str(file),size_hint=(None, None),height=size_y,width=size_x, pos_hint={'x': 0, 'y': 1},background_normal=x)
                    button.bind(on_release=partial(self.VideoContainer, file))      

                print file
                    self.scrollview.content_layout.add_widget(button)
                print button.id
                print("Parent of ScreenTwo: {}".format(self.parent)) 
                    #print(button.pos)

    def VideoContainer(self,name,btn):
        global isThereVideo
        if isThereVideo==True:
                #self.videocontainer.video_layout.unload()
                self.videocontainer.clear_widgets()



        mylist=name.split('.')
        emailButton = Button(id='email')
        video = Video(source="/home/linux/kivyFiles/kivyLogin/videoAssets/"+mylist[0]+".mp4", state='play',options={'eos': 'loop'})
        video.size=(self.parent.width,self.parent.height)
        video_pos=(self.parent.x,self.parent.y)
        #video.pos_hint={'x': self.parent.width /2, 'y': self.parent.height/2}

        video.play=True
        #video.pos=(self.parent.width /2 , self.parent.height/2)
        #self.videocontainer.video_layout.add_widget(emailButton)

        self.videocontainer.add_widget(emailButton)

        emailButton.add_widget(video)

        isThereVideo=True
        print("Parent of ScreenTwo: {}".format(self.parent)) 
        return 0

class ScreenManagement(ScreenManager):
    pass


class VideoContain(Widget):
    pass            


class ScrollableContainer(ScrollView):

    pass


presentation = Builder.load_file("login.kv")

class MainApp(App):
    def build(self):
        return presentation
if __name__ == '__main__':


    Window.fullscreen = True
    app=MainApp()
    app.run()

And my Kv file

ScreenManagement:

    MainScreen:
    Selfie:


<MainScreen>:
    name: 'main'

    Button:
        on_release: app.root.current = 'selfie'
        text: 'Another Screen'
        font_size: 50



<Selfie>:
    name: 'selfie'

    Button:
        on_release: app.root.current = 'login'
        text: 'Selfie Screen'
        font_size: 10
        pos_hint: {"right": 1, 'top':1}
        size_hint: (0.1, 0.1)
    RootWidget:


<RootWidget>
    size_hint: (0.1, None)
    scrollview: scrollview
    videocontainer:videocontainer
    size:(self.parent.width, self.parent.height)
    VideoContain:
        id:videocontainer
        ##size:(self.parent.width, self.parent.height)
        size:(root.width, root.height)
    ScrollableContainer:
        id: scrollview
        size: root.size
        pos: root.pos


<VideoContain>

    video_layout:video_layout
    FloatLayout:
        cols:1
        id: video_layout



<ScrollableContainer>:
    scroll_timeout: 75
    scroll_distance: 10
    app: app
    content_layout: content_layout
    GridLayout:
        id: content_layout
        cols: 1
        size_hint: (0.1, None)
        pos: root.pos
        height: self.minimum_height
        padding: 25
        spacing: 25

I posted all my code since i don't know which part may causing the problem i'm facing.

回答1:

Solved it.

def VideoContainer(self,name,btn):
    global isThereVideo
    if isThereVideo:
        # self.videocontainer.video_layout.unload()
        self.videocontainer.clear_widgets()

In this part i'm clearing the widgets but i also need to unload the video somehow.

self.video.unload() solved my problem