Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this questionIs it possible to open an mp3 file in Python (possible using Popen
) and I don't mean to run it in the program I mean as a separate window in media player or whatever just for it to open it when I call the function and if so how?
Opening a file with its associated application (Windows only):
import os
os.startfile('my_mp3.mp3')
A link to the documentation can be found here.
Here is the Python docs for Python in Music: http://wiki.python.org/moin/PythonInMusic
Listed there are libraries for opening and playing mp3, amongst other formats.
You could also use subprocess
. Then you would have to specify the path to the executable you want to run, which might not be helpful if you want this to work on someone else's computer, but is generally quite a powerful technique.
Usage:
import subprocess
PLAYERPATH = "C:/Program Files (x86)/VideoLAN/VLC/vlc.exe"
subprocess.call([PLAYERPATH, FILEPATH])
If you have vlc already installed on your system, then you can use the cvlc command.
import os
os.system('cvlc path/to/foo.mp3')
That will work. Hope it helps.
import mp3play,time
data= r'pathname'
clip = mp3play.load(data)
clip.play()
time.sleep(20)
clip.stop()
# Just listen to all the mp3 files in order
import os
folder=os.listdir(os.getcwd())
for files in folder:
if files.endswith(".mp3"):
os.startfile(files)
This script will pick a random song in the current directory. And will skip any file that is not a .mp3 file. You could add extra extensions to the list to be opened for example: ext3=['.mp3', '.mp4'] and so on.
import random,os,sys
folder=os.listdir(os.getcwd())
file=random.choice(folder)
ext3=['.mp3']
print('First random pick: '+file)
while file[-4:] not in ext3 :
print('Not an MP3 file : '+file)
file=random.choice(folder)
else:
os.startfile(file)
print('Song name: '+file)
sys.exit()
##os.startfile(random.choice(folder))
精彩评论