Jack Fishburn
Jack Fishburn

Reputation: 25

How can I play a sound through python regardless of file location?

I'm trying to play music through python:

import winsound

winsound.PlaySound("A", winsound.SND_FILENAME)
winsound.PlaySound("C", winsound.SND_FILENAME)
winsound.PlaySound("D", winsound.SND_FILENAME)
winsound.PlaySound("D", winsound.SND_FILENAME)

which works, but only while the python file is in the same folder as the sounds. Can I get it to play the sounds from anywhere? Or how do I properly import the sound from a specific path without the python file being with the sound files?

I tried:

   winsound.PlaySound("C:\Users\User_Name\Desktop\Microbit\A", 
   winsound.SND_FILENAME)     

I haven't done any coding in a while and I can't remember how to import things properly.

Upvotes: 0

Views: 495

Answers (1)

Robert Kearns
Robert Kearns

Reputation: 1706

Not familiar with the windsound library, but you usually need to escape the '\'. I imagine you also need the file type on the end of your string(.mp3, etc.) try:

winsound.PlaySound("C:\\Users\\User_Name\\Desktop\\Microbit\\A.file_type", #.mp3, .wav, etc
winsound.SND_FILENAME) 

Upvotes: 0

Related Questions