python - Kivy:SoundLoader 和加载多个文件,但不是 'double-load'

标签 python audio loading kivy

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

发生的情况是:前几个文件被加载,然后它跳过剩余的文件。

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

这是有问题的代码:

    #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 - 实习生“缓存管理器”
Cache Manager docs @ kivy.org
所以更新后的代码是这样的:

    #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 的帮助!

关于python - Kivy:SoundLoader 和加载多个文件,但不是 'double-load',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334486/

相关文章:

python - 如何将具有特定输入的函数传递给验证该输入的函数? - Python

python - 使用带有校验和的远程 tarball 安装软件包的 Saltstack 公式

c# - 加载图像停止问题

png - 加载动画 png

java - 使用 AssetManager libGDX 加载网格

python - 如何从 Python 中的 SQL 查询字符串中删除引号?

python - 两个函数,一个生成器

iOS 如何在录音时在扬声器中播放提示音

python-3.x - Pydub音频导出一开始就没有声音?

python - 如何在某些C代码执行某个线程时保持python脚本打开?