Python Forum
how do i play an mp3 from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i play an mp3 from a list
#1
i get how to play a mp3 with python vlc no problem what i am trying to figure out is the best way to store a album and be able to call any song from the list of tuples or from the dictionary that the album is stored in..so for example i could store an album in a list of tuples ,then assign the songs to variables that could be called by the track titles, but this seems incorrect..

import vlc

albums = [
 ("Abby road" , "The Beatles" , 1969,
          [
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/01-Come Together.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/02-Something.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/03 - Maxwells Silver Hammer.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/04 - Oh! Darling.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/05 - Octopuss Garden.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/06-I Want You .mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/07 - Here Comes The Sun.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/08 - Because.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/09 - You Never Give Me Your Money.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/10 - Sun King.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/11 - Mean Mr. Mustard.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/12 - Polythene Pam.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/13 - She Came In Through The Bathroom Window.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/14 - Golden Slumbers.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/15 - Carry That Weight.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/16 - The End.mp3"),
            ("/home/gr3yali3n/Music/The Beatles - Abbey Road/17 - Her Majesty.mp3"),
           ] ),

bathroom = albums[0][3][12]

media = vlc.MediaPlayer(bathroom)
media.play()
and here is the dictionary version this is the other way i was told to do it but I am not so sure as to how i am supposed to be able to point to the position of the stored mp3 file on the hdd for vlc to play when i call it.

albums = {"The beatles" :
          {"Abby Road": ["come together","something","maxwell's silver hammer","oh! darling",
                         "octopuss garden","i want you (shes so heavy)","here comes the sun","becdause",
                         "you neve give me your money","sun king","mean mr.mustard","polythene pam",
                         "she came in through the bathroom window","golden slumbers","carry that weight",
                         "the end","her majesty"]}}
Reply
#2
note that what you have as songs is not list of tuples, just list of strings. In this ("/home/gr3yali3n/Music/The Beatles - Abbey Road/01-Come Together.mp3") brackets are ignored. In order this to be a one-element tuple ir has to be ("/home/gr3yali3n/Music/The Beatles - Abbey Road/01-Come Together.mp3", ) - note the extra comma.

As to your question - a dictionary looks a better structure. probably complex nested structure, e.g. dict of dicts etc. or something like this. You may also find convenient to create class for album, song or use namedtuple, etc. it really depends what you want to do.
Larz60+ likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
the vlc command for playing the file is:
player = vlc.MediaPlayer(filepath)
I agree with Buran, you should use a dictionary. Much easier data access
Reply
#4
You can make a list from the album folder, for example (i am using pydub for audio)

import os
from pydub import AudioSegment
from pydub.playback import play

albums = "/home/gr3yali3n/Music/The Beatles - Abbey Road"

tracklist = []

for name in os.listdir(albums):
    file = os.path.join(albums, name)
    if file.endswith(".mp3"):
        tracklist.append(os.path.join(albums, name))

bathroom = [0, 3, 12] 

for a in bathroom:
    print(tracklist[a])
    song = AudioSegment.from_file(tracklist[a], "mp3")
    play(song)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I send a .mp3 to play through my mic? ejected 20 20,173 Sep-01-2022, 02:38 AM
Last Post: victormayer
  Play the next music in a list Pymax 0 1,200 Jul-28-2021, 07:27 PM
Last Post: Pymax
  How to play against the computer Help_me_Please 4 4,054 Aug-29-2019, 03:37 PM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020