本文整理汇总了Python中pygame.mixer.Sound方法的典型用法代码示例。如果您正苦于以下问题:Python mixer.Sound方法的具体用法?Python mixer.Sound怎么用?Python mixer.Sound使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame.mixer
的用法示例。
在下文中一共展示了mixer.Sound方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_sound
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def get_sound(*names, **kwds):
if sound_cache is None:
return dummy_sound
path = _resource_path("sounds", names, **kwds)
sound = sound_cache.get(path)
if not sound:
try:
from pygame.mixer import Sound
except ImportError, e:
no_sound(e)
return dummy_sound
try:
sound = Sound(path)
except pygame.error, e:
missing_sound(e, path)
return dummy_sound
示例2: __init__
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def __init__(self, colour, pos, text, handler=None):
self.handler = handler
image = pygame.image.load("assets/button.png").convert()
size = (image.get_rect().width, image.get_rect().height)
font = Font("assets/swiss911.ttf", 19)
textImage = font.render(text, False, colours.BLACK)
image.blit(textImage,
(image.get_rect().width - textImage.get_rect().width - 10,
image.get_rect().height - textImage.get_rect().height - 5))
self.image = image
self.colour = colour
LcarsWidget.__init__(self, colour, pos, size)
self.applyColour(colour)
self.highlighted = False
self.beep = Sound("assets/audio/panel/202.wav")
示例3: update
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def update() -> None:
"""Updates the volume of the sounds and music."""
# Volume has to be in 0.0 - 1.0 range
if user_data.sound_mute:
user_data.sound_volume = 0
sound_vol = 0
else:
sound_vol = user_data.sound_volume / 100
if user_data.music_mute:
user_data.music_volume = 0
music_vol = 0
else:
music_vol = user_data.music_volume / 100
Sound.click.set_volume(sound_vol)
Sound.check.set_volume(sound_vol)
Sound.task_completed.set_volume(sound_vol)
Sound.task_failed.set_volume(sound_vol)
Sound.task_click.set_volume(sound_vol)
Sound.game_over.set_volume(sound_vol)
mixer.music.set_volume(music_vol)
示例4: get_sound
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def get_sound(*names, **kwds):
if sound_cache is None:
return dummy_sound
path = _resource_path("sounds", names, **kwds)
sound = sound_cache.get(path)
if not sound:
try:
from pygame.mixer import Sound
except ImportError as e:
no_sound(e)
return dummy_sound
try:
sound = Sound(path)
except pygame.error as e:
missing_sound(e, path)
return dummy_sound
sound_cache[path] = sound
return sound
示例5: _test_array_argument
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def _test_array_argument(self, format, a, test_pass):
from numpy import array, all as all_
try:
snd = mixer.Sound(array=a)
except ValueError:
if not test_pass:
return
self.fail("Raised ValueError: Format %i, dtype %s" %
(format, a.dtype))
if not test_pass:
self.fail("Did not raise ValueError: Format %i, dtype %s" %
(format, a.dtype))
a2 = array(snd)
a3 = a.astype(a2.dtype)
lshift = abs(format) - 8 * a.itemsize
if lshift >= 0:
# This is asymmetric with respect to downcasting.
a3 <<= lshift
self.assertTrue(all_(a2 == a3),
"Format %i, dtype %s" % (format, a.dtype))
示例6: todo_test_find_channel
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def todo_test_find_channel(self):
# __doc__ (as of 2008-08-02) for pygame.mixer.find_channel:
# pygame.mixer.find_channel(force=False): return Channel
# find an unused channel
#
# This will find and return an inactive Channel object. If there are
# no inactive Channels this function will return None. If there are no
# inactive channels and the force argument is True, this will find the
# Channel with the longest running Sound and return it.
#
# If the mixer has reserved channels from pygame.mixer.set_reserved()
# then those channels will not be returned here.
#
self.fail()
示例7: todo_test_play
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def todo_test_play(self):
# __doc__ (as of 2008-08-02) for pygame.mixer.Channel.play:
# Channel.play(Sound, loops=0, maxtime=0, fade_ms=0): return None
# play a Sound on a specific Channel
#
# This will begin playback of a Sound on a specific Channel. If the
# Channel is currently playing any other Sound it will be stopped.
#
# The loops argument has the same meaning as in Sound.play(): it is
# the number of times to repeat the sound after the first time. If it
# is 3, the sound will be played 4 times (the first time, then three
# more). If loops is -1 then the playback will repeat indefinitely.
#
# As in Sound.play(), the maxtime argument can be used to stop
# playback of the Sound after a given number of milliseconds.
#
# As in Sound.play(), the fade_ms argument can be used fade in the sound.
self.fail()
示例8: todo_test_queue
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def todo_test_queue(self):
# __doc__ (as of 2008-08-02) for pygame.mixer.Channel.queue:
# Channel.queue(Sound): return None
# queue a Sound object to follow the current
#
# When a Sound is queued on a Channel, it will begin playing
# immediately after the current Sound is finished. Each channel can
# only have a single Sound queued at a time. The queued Sound will
# only play if the current playback finished automatically. It is
# cleared on any other call to Channel.stop() or Channel.play().
#
# If there is no sound actively playing on the Channel then the Sound
# will begin playing immediately.
#
self.fail()
示例9: test_samples_address
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def test_samples_address(self):
"""Test the _samples_address getter."""
from ctypes import pythonapi, c_void_p, py_object
try:
Bytes_FromString = pythonapi.PyBytes_FromString # python 3
except:
Bytes_FromString = pythonapi.PyString_FromString # python 2
Bytes_FromString.restype = c_void_p
Bytes_FromString.argtypes = [py_object]
samples = as_bytes('abcdefgh') # keep byte size a multiple of 4
sample_bytes = Bytes_FromString(samples)
snd = mixer.Sound(buffer=samples)
self.assertNotEqual(snd._samples_address, sample_bytes)
示例10: test_set_volume
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def test_set_volume(self):
"""Ensure a sound's volume can be set."""
float_delta = 1.0 / 128 # SDL volume range is 0 to 128
filename = example_path(os.path.join('data', 'house_lo.wav'))
sound = mixer.Sound(file=filename)
current_volume = sound.get_volume()
# (volume_set_value : expected_volume)
volumes = ((-1, current_volume), # value < 0 won't change volume
(0, 0.0),
(0.01, 0.01),
(0.1, 0.1),
(0.5, 0.5),
(0.9, 0.9),
(0.99, 0.99),
(1, 1.0),
(1.1, 1.0),
(2.0, 1.0))
for volume_set_value, expected_volume in volumes:
sound.set_volume(volume_set_value)
self.assertAlmostEqual(sound.get_volume(), expected_volume,
delta=float_delta)
示例11: test_sound_unicode
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def test_sound_unicode(self):
"""test non-ASCII unicode path"""
mixer.init()
import shutil
ep = unicode_(example_path('data'))
temp_file = os.path.join(ep, u'你好.wav')
org_file = os.path.join(ep, u'house_lo.wav')
shutil.copy(org_file, temp_file)
try:
with open(temp_file, 'rb') as f:
pass
except IOError:
raise unittest.SkipTest('the path cannot be opened')
try:
sound = mixer.Sound(temp_file)
del sound
finally:
os.remove(temp_file)
示例12: no_sound
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def no_sound(e):
global sound_cache
print "albow.resource.get_sound: %s" % e
print "albow.resource.get_sound: Sound not available, continuing without it"
sound_cache = None
示例13: _play_sound
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def _play_sound(self, sound):
"""
Play a sound.
:param sound: Sound to be played
:type sound: :py:class:`pygame.mixer.Sound`, None
:return: True if the sound was played
:rtype: bool
"""
if not sound:
return False
# Find an available channel
channel = self.get_channel() # This will set the channel if it's None
if channel is None: # The sound can't be played because all channels are busy
return False
# Play the sound
soundtime = time.time()
# If the previous sound is the same and has not ended (max 20% overlap)
if sound['type'] != self._last_play or \
soundtime - self._last_time >= 0.2 * sound['length'] or self._uniquechannel:
try:
if self._uniquechannel: # Stop the current channel if it's unique
channel.stop()
channel.play(sound['file'],
loops=sound['loops'],
maxtime=sound['maxtime'],
fade_ms=sound['fade_ms']
)
except pygame_error: # Ignore errors
pass
# Store last execution
self._last_play = sound['type']
self._last_time = soundtime
return True
示例14: load
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def load(self):
self.unload()
if self.filename is None:
return
self._data = mixer.Sound(self.filename)
示例15: __init__
# 需要导入模块: from pygame import mixer [as 别名]
# 或者: from pygame.mixer import Sound [as 别名]
def __init__(self, source):
if config.SOUND:
self.sound = OldSound(source)