我的意图是让的两首音乐曲目,本质上是相似的,在不同的时间在彼此的之间褪色。当出现这种淡出时,一首音乐曲目应在短时间内从全音量逐渐减弱为静音,同时,另一首曲目应从0减弱到100,并从同一时间索引继续播放。他们必须能够在任何时候做这个动态--当某个动作发生时,会发生淡出,而新曲目将开始在另一个停止播放的位置上播放。
通过使用音量操作或启动和停止音乐(然而,似乎只有"fadeout“选项存在,并且缺少"fadein”选项),这可能是合理的。我该怎么做?如果有的话,最好的方法是什么?如果不可能使用Pygame,那么替代Pygame是可以接受的。
发布于 2013-09-18 10:11:01
试试这个,挺直的..
import pygame
pygame.mixer.init()
pygame.init()
# Maybe you can subclass the pygame.mixer.Sound and
# add the methods below to it..
class Fader(object):
instances = []
def __init__(self, fname):
super(Fader, self).__init__()
assert isinstance(fname, basestring)
self.sound = pygame.mixer.Sound(fname)
self.increment = 0.01 # tweak for speed of effect!!
self.next_vol = 1 # fade to 100 on start
Fader.instances.append(self)
def fade_to(self, new_vol):
# you could change the increment here based on something..
self.next_vol = new_vol
@classmethod
def update(cls):
for inst in cls.instances:
curr_volume = inst.sound.get_volume()
# print inst, curr_volume, inst.next_vol
if inst.next_vol > curr_volume:
inst.sound.set_volume(curr_volume + inst.increment)
elif inst.next_vol < curr_volume:
inst.sound.set_volume(curr_volume - inst.increment)
sound1 = Fader("1.wav")
sound2 = Fader("2.wav")
sound1.sound.play()
sound2.sound.play()
sound2.sound.set_volume(0)
# fading..
sound1.fade_to(0)
sound2.fade_to(1)
while True:
Fader.update() # a call that will update all the faders..发布于 2017-02-03 16:13:44
这并不完全是对这个问题的回答,但对于未来的谷歌人来说,我写了一个要淡出的脚本--在我早上的第0卷的音乐中,这就是我所用的:
max_volume = 40
current_volume = 0
# set the volume to the given percent using amixer
def set_volume_to(percent):
subprocess.call(["amixer", "-D", "pulse", "sset", "Master",
str(percent) + "%", "stdout=devnull"])
# play the song and fade in the song to the max_volume
def play_song(song_file):
global current_volume
print("Song starting: " + song_file)
pygame.mixer.music.load(song_file)
pygame.mixer.music.play()
# gradually increase volume to max
while pygame.mixer.music.get_busy():
if current_volume < max_volume:
set_volume_to(current_volume)
current_volume += 1
pygame.time.Clock().tick(1)
play_song("foo.mp3")发布于 2013-09-16 13:36:20
伪码:
track1 = ...
track2 = ...
track1.play_forever()
track1.volume = 100
track2.play_forever()
track2.volume = 0
playing = track1
tracks = [track1, track2]
def volume_switcher():
while True:
playing.volume = min(playing.volume + 1, 100)
for track in tracks:
if track != playing:
track.volume = max(track.volume - 1, 100)
time.sleep(0.1)
Thread(target=volume_switcher).start()https://stackoverflow.com/questions/18733213
复制相似问题