5 examples of 'how to use playsound in python' in Python

Every line of 'how to use playsound 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
this disclaimer
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()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

78def playSound(num):
79 for x in range(num):
80 t = threading.Timer(0.5*x, lambda: subprocess.Popen(["aplay", soundPath]))
81 t.start()
4def _playsoundWin(sound, block = True):
5 '''
6 Utilizes windll.winmm. Tested and known to work with MP3 and WAVE on
7 Windows 7 with Python 2.7. Probably works with more file formats.
8 Probably works on Windows XP thru Windows 10. Probably works with all
9 versions of Python.
10
11 Inspired by (but not copied from) Michael Gundlach <gundlach@gmail.com>'s mp3play:
12 https://github.com/michaelgundlach/mp3play
13
14 I never would have tried using windll.winmm without seeing his code.
15 '''
16 from ctypes import c_buffer, windll
17 from random import random
18 from time import sleep
19
20 def winCommand(*command):
21 buf = c_buffer(255)
22 command = ' '.join(command).encode()
23 errorCode = int(windll.winmm.mciSendStringA(command, buf, 254, 0))
24 if errorCode:
25 errorBuffer = c_buffer(255)
26 windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254)
27 exceptionMessage = ('\n Error ' + str(errorCode) + ' for command:'
28 '\n ' + command +
29 '\n ' + errorBuffer.value)
30 raise PlaysoundException(exceptionMessage)
31 return buf.value
32
33 alias = 'playsound_' + str(random())
34 winCommand('open "' + sound + '" alias', alias)
35 winCommand('set', alias, 'time format milliseconds')
36 durationInMS = winCommand('status', alias, 'length')
37 winCommand('play', alias, 'from 0 to', durationInMS)
38
39 if block:
40 sleep(float(durationInMS) / 1000.0)
33def test_sound4(self):
34 rv = wx.adv.Sound.PlaySound(wavFile, wx.adv.SOUND_SYNC)
169def playSound(self, path, volume = 1):
170 pathes = game_settings['sfxpathes']
171 types = game_settings['soundtypes']
172 for ft in ((folder,type) for folder in pathes for type in types):
173 if exists(ft[0] + path + ft[1]):
174 path = ft[0] + path + ft[1]
175 break
176 play(path,self.sfxMgr, volume = volume)

Related snippets