10 examples of 'play music in python' in Python

Every line of 'play music 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
22def play(song_name): #音乐播放器
23
24 print('正在播放:' + song_name)
25 os.system('mplayer /home/pi/xiaolan/memory_center/musiclib/music.mp3')
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

31def play(self):
32 if self.sounds[self.level] is not None:
33 self.sounds[self.level].play(loops=self.loops)
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()
32def play_mp3_file(mp3_file):
33 # set output high in order to turn on amplifier
34 GPIO.output(amp_switch_pin, 1)
35 time.sleep(0.3)
36 pygame.mixer.init()
37 pygame.mixer.music.load(mp3_file)
38 print "now playing file: ", mp3_file
39 pygame.mixer.music.play()
40 while pygame.mixer.music.get_busy():
41 time.sleep(0.1)
42 if button_pressed == True:
43 pygame.mixer.music.stop()
44 pygame.mixer.quit()
45 print 'alarm turned off'
46 break
47 else:
48 continue
49 # set ouput low in order to turn off amplifier
50 time.sleep(0.5)
51 GPIO.output(amp_switch_pin, 0)
52 pygame.mixer.quit()
53 print 'quit play function'
147def cmd_play(self):
148 self.cmd_resume()
149 self.fsg.sync(self.fsg.show)
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"""
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
37def play_file(wavfile):
38 f = open(wavfile, "rb")
39 wav = audioio.WaveFile(f)
40 a.play(wav)
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()
7def play(file_name):
8 import pyglet
9 import os
10 dll_file_name = os.path.abspath('avbin')
11 pyglet.lib.load_library(dll_file_name)
12
13 player = pyglet.media.Player()
14 source = pyglet.media.load(file_name)
15 player.queue(source)
16
17 player.play()
18
19 def update(dt):
20 if not player.playing:
21 # Отпишем функцию, иначе при повторном вызове, иначе
22 # будет двойной вызов при следующем воспроизведении
23 pyglet.clock.unschedule(update)
24 pyglet.app.exit()
25
26 # Every 500 ms / 0.5 sec
27 pyglet.clock.schedule_interval(update, 0.5)
28 pyglet.app.run()

Related snippets