Skip to content Skip to sidebar Skip to footer

Cannot Execute .mp3 File

I'm trying to make a script for Python that basically plays a sound every five seconds. My code: import time import os while True: path = '/Users/ColShell/Desktop/beep-08b.mp

Solution 1:

You're attempting to execute an MP3 file so of course it's throwing that error - try pasting /Users/ColShell/Desktop/beep-08b.mp3 in your Terminal and see what happens.

Are you trying to play the file in the default system player instead? That would depend on your OS - I'd assume MacOS X based on your path, so you can use open to call the default application for MP3 files:

import subprocess

subprocess.call(["open", "/Users/ColShell/Desktop/beep-08b.mp3"])

However, it would be better to use an app designed to play sounds in the background and OSX has afplay for that, so:

import subprocess

subprocess.call(["afplay", "/Users/ColShell/Desktop/beep-08b.mp3"])

But if you want to play your sounds directly in Python then there are a couple of options - I personally find playsound to be one of the most elegant approaches so install it and then you can easily use it with:

import playsound

playsound.playsound("/Users/ColShell/Desktop/beep-08b.mp3")

As a bonus - it's (mostly) cross platform.

Post a Comment for "Cannot Execute .mp3 File"