10 examples of 'how to play sounds in python' in Python

Every line of 'how to play sounds in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
24def play_sound(self):
25 try:
26 logging.info("Playing sound button")
27 # -- Play the sound --
28 pygame.mixer.Sound(SOUND_PATH + self.sound_name).play()
29 # -- End play the sound --
30 logging.info("End of sound button")
31 except:
32 logging.info("Fail to open the sound")
152def play_sound(filename):
153 """Play the given sound file using the `afplay` command line utility."""
154 subprocess.Popen(['afplay', filename])
50def playmusic2(soundfile):
51 """Stream music with mixer.music module using the event module to wait
52 until the playback has finished.
53
54 This method doesn't use a busy/poll loop, but has the disadvantage that
55 you neet to initialize the video module to use the event module.
56
57 Also, interrupting the playback with Ctrl-C does not work :-(
58
59 Change the call to 'playmusic' in the 'main' function to 'playmusic2'
60 to use this method.
61 """
62
63 pygame.init()
64
65 pygame.mixer.music.load(soundfile)
66 pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
67 pygame.event.set_allowed(pygame.constants.USEREVENT)
68 pygame.mixer.music.play()
69 pygame.event.wait()
36def play_wav(cfn):
37
38 global wav16_dir, tts, corpus
39
40 wavfn = '%s/%s/%s.wav' % (wav16_dir, corpus, cfn)
41
42 with open(wavfn) as wavf:
43 wav = wavf.read()
44
45 tts.play_wav(wav, async=True)
37def play_file(wavfile):
38 f = open(wavfile, "rb")
39 wav = audioio.WaveFile(f)
40 a.play(wav)
11def play_file(wavfile):
12 audio_file = open(wavfile, "rb")
13 wav = audioio.WaveFile(audio_file)
14 speaker.play(wav)
15 while speaker.playing:
16 pass
189def do_playback():
190
191 global recording, buf
192
193 buf = StringIO()
194
195 wf = wave.open(buf, 'wb')
196 wf.setnchannels(1)
197 wf.setsampwidth(2)
198 wf.setframerate(SAMPLE_RATE)
199
200 packed_audio = struct.pack('%sh' % len(recording), *recording)
201 wf.writeframes(packed_audio)
202
203 player.play(buf.getvalue())
204
205 buf.close()
143def __reduce_ex__(self,*args):
144 pass
145 def __str__(self,*args):
146 pass
147 CanRaiseEvents=property(lambda self: object(),lambda self,v: None,lambda self: None)
148 """Gets a value indicating whether the component can raise an event.
149
150"""
72def _playsoundNix(sound, block = True):
73 '''
74 Utilizes ossaudiodev. Untested. Probably works with all version of Linux
75 with Python 2.3 or newer.
76
77 Inspired by, and more or less copied from, Bill Dandreta's post on
78 this mailing list (since deleted, so I link to a web archive instead):
79 https://web.archive.org/web/20080218155209/http://mail.python.org/pipermail/python-list/2004-October/288905.html
80 '''
81 import ossaudiodev
82 from sys import byteorder
83 from wave import open as waveOpen, AFMT_S16_LE, AFMT_S16_BE
84
85 with waveOpen(sound, 'rb') as sound:
86 channelCount, sampleWidth, framerate, frameCount, compressionType, compressionName = sound.getparams()
87 try:
88 from ossaudiodev import AFMT_S16_NE
89 except ImportError:
90 if 'little' in byteorder.lower():
91 AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
92 else:
93 AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
94 data = sound.readframes(frameCount)
95
96 speaker = ossaudiodev.open('/dev/dsp', 'w')
97 speaker.setparameters(AFMT_S16_NE, channelCount, framerate)
98 speaker.write(data)
99 speaker.close()
89def play(self, track=None):
90 pass

Related snippets