Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy:SoundLoader并加载多个文件,但不是';双负载';_Python_Audio_Loading_Kivy - Fatal编程技术网

Python Kivy:SoundLoader并加载多个文件,但不是';双负载';

Python Kivy:SoundLoader并加载多个文件,但不是';双负载';,python,audio,loading,kivy,Python,Audio,Loading,Kivy,我想通过SoundLoader模块在kivy中加载多个声音文件(*.ogg)。文件大小从300kB到700kB 发生的情况是:加载前两个文件,然后跳过其余文件 有没有更好(也许更快)的方法来加载文件?是否可以将已加载的文件“链接”到另一个按钮实例,而不复制它(或破坏它的文本) 下面是有问题的代码: #Getting filenames: for line in rawsongs: if ',' in line: items = line.

我想通过SoundLoader模块在kivy中加载多个声音文件(*.ogg)。文件大小从300kB到700kB

发生的情况是:加载前两个文件,然后跳过其余文件

有没有更好(也许更快)的方法来加载文件?是否可以将已加载的文件“链接”到另一个按钮实例,而不复制它(或破坏它的文本)

下面是有问题的代码:

    #Getting filenames:

    for line in rawsongs:
        if ',' in line:
            items = line.split(', ')

            #Creating instances of Buttons, which control (play and stop)
            #the soundfiles:

            btn = AudioButton(
            text=(items[1]+' - '+items[2]), font_size=50, 
            sound = SoundLoader.load(items[2]+'.ogg'), 
            size_hint_y = None, height = 240, group = 'audio')

            #adding the Button to the Layout:
            grid.add_widget(btn)
        else:
            pass

提前感谢;)

在与来自#kivy的人进行了一些IRC之后,有人建议我使用kivy实习生“缓存管理器” 因此,更新后的代码如下所示:

    #NEW: Registering the Cache

    Cache.register('songcache', timeout = 100)

    #Getting filenames:

    for line in rawsongs:
        if ',' in line:
            items = line.split(', ')

            #NEW: Check if the file is already cached:

            if Cache.get('songcache', items[2]) == None:

                #Setting up the button:

                btn = AudioButton(
                    text=(items[1]+' - '+items[2]), font_size=50, 
                    sound = SoundLoader.load(items[2]+'.ogg'), 
                    size_hint_y = None, height = 240, group = 'metro')

                #NEW: Adding the instance of the soundfile to the cache:

                Cache.append('songcache', items[2], btn.sound)

                grid.add_widget(btn)
            else:
                btn = AudioButton(
                    text=(items[1]+' - '+items[2]), font_size=50, 

                    #NEW: Linking the previously Cached instance with the new Button

                    sound = Cache.get('songcache', items[2]), 
                    size_hint_y = None, height = 240, group = 'metro')
                grid.add_widget(btn)           
        else:
            pass
所以,感谢来自#kivy的家伙们的帮助