CurrentSound = 'CarEngine0.wav'oldSound = CurrentSoundwhile True: CurrentSound = 'CarEngine' + str(abs(int(speed*2))) + '.wav' CarSound = pygame.mixer.sound(CurrentSound) if not oldSound == CurrentSound: oldSound = CurrentSound CarSound.stop() CarSound.play(-1)
如果我打电话给CurrentSound和oldSound变量打印,它们会改变,但声音似乎没有.我做错了什么,有没有更好的方法呢?
解决方法 如果我理解正确,你有25个声音文件,你想按照速度变量的递增顺序播放它们. 25个声音文件名称根据其音高进行排序.建立
该解决方案假设您将音频文件放在一个文件夹中,并按顺序逐渐按顺序列出.
代码将根据速度计数器加载在文件夹中按顺序列出的文件[您可以根据需要进行修改]
代码是在windows 8和python 2.7上编写的
示例代码
import pygame.mixer,pygame.timeimport osmixer = pygame.mixer mixer.init() #Initialize mixer#Your path to audio filesfilepath = "C:\yourAudiofilePath\"#Iterate through counter and audio filesfor x,i in zip(range(0,25),os.Listdir(filepath)): if i.endswith(".wav"): mySoundfile = mixer.sound(filepath + i) print "Speed Variable = ",x," and file = ",i channel = mySoundfile.play(0) while channel.get_busy(): #Check if Channel is busy pygame.time.wait(100) # wait in ms until song is played print "........"
产量
我按顺序重命名了一堆音频文件以符合您的要求.
Python 2.7.9 (default,Dec 10 2014,12:24:55) [MSC v.1500 32 bit (Intel)] on win32Type "copyright","credits" or "license()" for more information.>>> ================================ RESTART ================================>>> Speed Variable = 0 playing file = audio000.wav........Speed Variable = 1 playing file = audio001.wav........Speed Variable = 2 playing file = audio002.wav........Speed Variable = 3 playing file = audio003.wav........Speed Variable = 5 playing file = audio005.wav........Speed Variable = 6 playing file = audio006.wav........Speed Variable = 7 playing file = audio007.wav........Speed Variable = 8 playing file = audio008.wav........Speed Variable = 9 playing file = audio009.wav........Speed Variable = 10 playing file = audio0010.wav........Speed Variable = 11 playing file = audio0011.wav........Speed Variable = 12 playing file = audio0012.wav........Speed Variable = 13 playing file = audio0013.wav........Speed Variable = 14 playing file = audio0014.wav........Speed Variable = 15 playing file = audio0015.wav........Speed Variable = 16 playing file = audio0016.wav........Speed Variable = 17 playing file = audio0017.wav........Speed Variable = 18 playing file = audio0018.wav........Speed Variable = 19 playing file = audio0019.wav........Speed Variable = 20 playing file = audio0020.wav........Speed Variable = 21 playing file = audio0021.wav........Speed Variable = 22 playing file = audio0022.wav........Speed Variable = 23 playing file = audio0023.wav........Speed Variable = 24 playing file = audio0024.wav........>>>总结
以上是内存溢出为你收集整理的python – 改变声音效果Pygame全部内容,希望文章能够帮你解决python – 改变声音效果Pygame所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)