预告片中的声音 [英] Preloading sound in kivy

查看:62
本文介绍了预告片中的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一款基于奇异果的游戏,应该可以播放一些声音效果.声音对象的加载方式如下:

I have a kivy-based game that is supposed to play some sound FX. Sound objects are loaded like this:

self.boombox = {'moved': SoundLoader.load('dshoof.wav'),
                'attacked': SoundLoader.load('dspunch.wav')}

并在适当的时候像这样播放:

And played whenever appropriate like this:

self.parent.boombox['attacked'].play()

大多数情况下都有效,但是第一次播放任何特定声音时,它会滞后半秒.我想这是将WAV从磁盘加载到内存所需要的时间.有什么方法可以确保在初始化过程中加载声音,而不是看起来像是在偷懒? 如果有任何相关性,则在基于Linux的PC(非Android)上会观察到此行为.

It mostly works, but the first time any particular sound is played, it lags for about half a second. I guess that's the time it takes to load WAV from disk to memory. Is there any way to make sure sounds are loaded during initialization, not in a lazy manner it seems to be? This behaviour is observed on Linux-based PC, non Android, if that's of any relevance.

推荐答案

事实证明,它可以被黑客入侵.我需要做的就是将播放器显式设置为文件的开头:

It can be hacked around, as it turned out. All I needed was to set the player to the start of the file explicitly:

self.boombox = {'moved': SoundLoader.load('dshoof.wav'),
                'attacked': SoundLoader.load('dspunch.wav')}
for sound in self.boombox.keys():
    self.boombox[sound].seek(0)

因为无论如何都会做类似的事情,所以声音没有任何改变.但是,它强制声音提供者立即读取文件,而不是等到它被调用后再读取.而且,当然,这可以在关卡加载过程中轻松完成,而不必搞乱游戏玩法.

As something like that would've been done anyway, it changes nothing about the sound. However, it forces sound provider to read the file right now instead of waiting until it's called. And, of course, this can be easily done during level loading instead of messing with the gameplay.

这篇关于预告片中的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆