Playsound模块无法播放音乐文件

时间:2020-10-20 14:22:14

标签: python audio mp3 playsound

这是我的Python MP3播放器脚本。由于某种原因,它表示找不到文件。我尝试调试,发现一些错误。我解决了其中的一些问题,但之后,它使我进入了Playsound soruce代码,但我听不懂。我似乎不知道这是怎么回事。

这是代码:

import os
import random
from playsound import playsound


class Mp3Player:
    def __init__(self, path):
        self.path = path
        self.songs_list = None

    def display_songs(self):
        songs_list = []
        for dirpath, dirname, filename in os.walk(path):
            for file in filename:
                if file.endswith(".mp3"):
                    songs_list.append(file)
        j = 1
        for i in songs_list:
            index_of_mp3 = i.index(".mp3")
            display_songs = str(j) + ". " + i[:index_of_mp3]
            j += 1
            print(display_songs)
        self.songs_list = songs_list
        print(" ")


    def play_songby_number(self):

        song_choice = int(input("Enter the number of the song: "))
        print(self.songs_list[song_choice - 1])
        playsound(self.songs_list[song_choice - 1])

    def suffle_play(self):
        random_song = random.choice(self.songs_list)
        playsound(random_song)



path = input("Enter path: ")
my_mp3_player = Mp3Player(path)
my_mp3_player.display_songs()
choice_play = int(input("Please enter a choice: \n1. Play a song by it's number \n2. Shuffle play songs \nYour choice: "))
if choice_play == 1:
    my_mp3_player.play_songby_number()
elif choice_play == 2:
    my_mp3_player.suffle_play()
else:
    print("Please enter a valid number")

这是我得到的错误:

Enter path: D:\Python Projects\MP3-Player-Python\Songs
1. Billie Eilish, Khalid - lovely
2. Chase Atlantic - Into It
3. Chase Atlantic - Right Here
4. Chase Atlantic - Swim
5. DJ Snake - Magenta Riddim
6. Lil Nas X - Old Town Road ft. Billy Ray Cyrus
7. Luis Fonsi - Despacito ft. Daddy Yankee
8. Marshmello & Anne-Marie - FRIENDS
9. Wiz Khalifa - See You Again ft. Charlie Puth
 
Please enter a choice: 
1. Play a song by it's number 
2. Shuffle play songs 
Your choice: 1
Enter the number of the song: 1
Billie Eilish, Khalid - lovely.mp3
Traceback (most recent call last):
  File "D:/Python Projects/MP3-Player-Python/mp3_player_v1.0.py", line 44, in <module>
    my_mp3_player.play_songby_number()
  File "D:/Python Projects/MP3-Player-Python/mp3_player_v1.0.py", line 31, in play_songby_number
    playsound(self.songs_list[song_choice - 1])
  File "C:\Users\ravur\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 35, in _playsoundWin
    winCommand('open "' + sound + '" alias', alias)
  File "C:\Users\ravur\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 31, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException: 
    Error 275 for command:
        open "Billie Eilish, Khalid - lovely.mp3" alias playsound_0.06292091250508147
    Cannot find the specified file.  Make sure the path and filename are correct.

0 个答案:

没有答案