Simple music player

In this post I’m going to show you how to make a simple music player using python .

So Let’s get started….

Requirements:

  • import os
  • import time
  • import eyed3

If you get any error during import try installing them first.

Now create a variable path and specify a path to the directory of your songs:

  • path = 'path to your music directory'

Now you have to move to the music directory using os module:

  • os.chdir(path)

Now we will list the songs using a variable:

  • files = os.listdir(path=path)
  • songs = [fi for fi in files if fi.endswith(".mp3")]

Now create a variable to itirate over songs and now using for loop we will get to each song

and we will use eyed3 module to get the duration of the song so we can set a sleep timer to

move to the next song and we will also use os module to start the song:

  • i = 0
  • try:
  • for item in songs:
  • i+=1
  • duration = eyed3.load(path + "\" + songs[i]).info.time_secs
  • print(str(i) + f"--> {item} \n Duration---:: {duration} seconds \n")
  • d = songs[i]
  • os.startfile(d)
  • time.sleep(duration)

Now we will try to except any error if we get during this process so our program don’t get stopped in between middle:

  • except Exception as e:
  • print(e)
<p style="font-size:22px" value="<amp-fit-text layout="fixed-height" min-font-size="18" max-font-size="72" height="80"><strong>If you want to play a song in random manner you can remove the commented line:</strong>If you want to play a song in random manner you can remove the commented line:

  • d = random.choice(songs)

    import os, random,time,eyed3
    path = 'path to your songs directory'
    os.chdir(path)
    files = os.listdir(path=path)
    songs = [fi for fi in files if fi.endswith(".mp3")]
    # d = random.choice(songs)
    i = 0
    try:
        for item in songs:
            i+=1
            duration = eyed3.load(path + "\\" + songs[i]).info.time_secs
            print(str(i) + f"--> {item} \n Duration---::  {duration} seconds \n")
            d = songs[i]
            os.startfile(d)
            time.sleep(duration)
    except Exception as e:
        print(e)

If you liked this post don’t forget share it.