Python Forum
how do i play an mp3 from a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how do i play an mp3 from a list (/thread-31254.html)



how do i play an mp3 from a list - gr3yali3n - Nov-30-2020

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"]}}



RE: how do i play an mp3 from a list - buran - Nov-30-2020

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.


RE: how do i play an mp3 from a list - Larz60+ - Nov-30-2020

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


RE: how do i play an mp3 from a list - Axel_Erfurt - Dec-01-2020

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)