How Play to audio list in os.startfile use Python?

706 views Asked by At

How Play to audio list in os.startfile use Python?

MY CODE:

import os

music_dir = "C:\\Users\\JACKSON KASI\\Music"
songs = os.listdir(music_dir)
print(songs)

for i in songs:
    os.startfile(os.path.join(music_dir,i))

The next audio file should run after the first audio file is fully functional. But my code is play all audio fast. It's not perfectly. someone help me please.

Do you can use pygame (OR) python-vlc use to solve this proplem.

2

There are 2 answers

0
ShadowRanger On

os.startfile is just launching the file asynchronously in the background. Per the docs:

startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.

Basically, you can't use it for your purpose, because there's no way to know when it's done.

There are many, many walkthroughs for playing media files; please search the web for solutions (providing a complete solution from scratch is somewhat beyond the scope of a StackOverflow question).

1
aryan jadhav On
random = os.startfile(os.path.join(music_dir , songs[1])

this will work