Reputation: 1031
I'm writing a simple script that I would like to have notify me of an event with a sound file on my computer.
While I can play a sound file by doing something like:
import webbrowser
webbrowser.open("C:\Users\User\Desktop\Test\Test.mp3")
That will open up VLC or whatever media player I have, which could alt-tab me / interfere with whatever I was doing.
Is there a way to play the sound file without anything popping up? A perfect example of what I am looking to do would be using something like making a beep sound using winsound as such:
import winsound
Freq = 600 # Set Frequency To 600 Hertz
Dur = 800 # Set Duration To 800 ms == 0.8 second(s)
winsound.Beep(Freq,Dur)
Which makes a quick beeping sound without opening any new windows.
Upvotes: 1
Views: 3396
Reputation: 1
You should use pygame to solve it
def music(music):
pygame.mixer.init()
pygame.mixer.music.load(music)
pygame.mixer.music.play()
it will play sound without opening vlc media player
search google for more otherwise this will be enough to know
Upvotes: 0
Reputation: 1031
Solved by @eryksun in the comments with the solution of:
winsound.PlaySound(wav_path, winsound.SND_FILENAME | winsound.SND_ASYNC)
This was the best solution for I've seen for what I was looking for. It uses default libraries, only takes up a single straight-forward line, and does what I need it to.
Upvotes: 0
Reputation: 5714
This does the trick. Firstly, install pyglet:
pip install pyglet
Now download and install AVbin from here Do check where AVbin is installed as now you have to go to the installation directory and copy the avbin.dll to the directory where you saved your code.
Finally,run this code:
import pyglet
pyglet.lib.load_library('avbin')
pyglet.have_avbin=True
song = pyglet.media.load('filename.mp3')#your file name
song.play()
pyglet.app.run()
Make sure your music file is in the same directory as your code .py file. Did this from my experience and it worked.
Upvotes: 2