1 year ago

#375086

test-img

baumanager

How can I read a File Path in Python and then play that song?

So I'm programming a Music Player. Now, my problem is as follows: When I add some songs, via a button and click on the Play-Button, the music plays normally. Now I have added a function that you can import a playlist. When I do this, the user has to choose the playlist file, which is a .txt file with the following content i.e.: ('C:/Users/kevin/Music/y2meta.com - Dream Chaos - Emptiness (320 kbps).mp3', 'C:/Users/kevin/Music/y2meta.com - Emir Güngör - Healing (320 kbps).mp3', 'C:/Users/kevin/Music/y2meta.com - Harris & Ford x 2 Engel & Charlie - Amsterdam (Official Video) (320 kbps).mp3', 'C:/Users/kevin/Music/y2meta.com - Henry Neeson - Evanescent (320 kbps).mp3')

Those are Paths to a Musicfile. Now Pygame should load that content and play the song. My Code is as follows:

# Declaration of the Index Variable:
  current_song = 0
  index = current_song

   
# The Code of the Play-Button
    def play_song():
        pygame_init()
        try: 
            label['text'] = songs[index]
            pygame.mixer.music.play()
            check_event()
        except pygame.error:
            label['text'] = playlist_file_content[index]
            pygame.mixer.music.load(playlist_file_content[index])
            pygame.mixer.music.play()
            check_event()

# Declaration of the playlist_file_content variable
def add_playlist():
    global playlist_file_content
    playlist_file = str(filedialog.askopenfilename(initialdir=f'C:/Users/{username}/Music'))
    with open (playlist_file, 'r') as file:
        playlist_file_content = file.read()
        # The Print Function is just for test purposes
        print(playlist_file_content)

The error Message I get, which I want to pass through by the except statement:

pygame.error: No file 'a' found in working directory 'C:\Users\kevin\Dropbox\Musicplayer Project Folder'
  

I hope you can help me, and thank you for your help in Advance.

python

file

exception

pygame

audio-player

0 Answers

Your Answer

Accepted video resources