2

我制作了一个带有播放列表的音乐播放器,但播放列表中的歌曲无法播放,因为播放列表中只有歌曲的名称,而不是完整的 mp3 文件。你能告诉我如何处理这个问题吗?这是我的代码:

from Tkinter import *
import mp3play
import tkFileDialog
import Tkinter
import tkFont
import Tkinter as tk
#from PIL import ImageTk,Image

def open_file():                                #Opens a dialog box to open .mp3 file
    global music                                #then sends filename to file_name_label.
    global mp3
    global play_list
    filename.set (tkFileDialog.askopenfilename(defaultextension = ".mp3", filetypes=[("All Types", ".*"), ("MP3", ".mp3")]))
    playlist = filename.get()
    playlist_pieces = playlist.split("/")
    play_list.set (playlist_pieces[-1])
    playl = play_list.get()
    play_list_display.insert(END, playl)
    mp3 = filename.get()
    print mp3
    music = mp3play.load(mp3)
    pieces = mp3.split("/")
    name.set (pieces[-1])

def play():                                     #Plays the .mp3 file
    music.play()

def stop():                                     #Stops the .mp3 file
    music.stop()                

def pause():                                    #Pauses or unpauses the .mp3 file
    if music.ispaused() == True:
        music.unpause()
    elif music.ispaused() == False:
        music.pause()

def vol(event):                                 #Allows volume to be changed with the slider
    v = Scale.get(volume_slider)
    music.volume(v)

def tune_changed(event):
    idx = event.widget.curselection()[0]
    print ("Now playing %s" % event.widget.get(idx))

def Exit():
    exit()

root = tk.Tk()
root.title("EmoPlayer")

root.configure(background='black')

#root = Tk()

root.geometry('300x100+750+300')

filename = Tkinter.StringVar()
name = Tkinter.StringVar()
play_list = Tkinter.StringVar()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0, bg="black", fg="Orange")
menubar.add_cascade(label='File', menu = filemenu)
filemenu.add_command(label='Open', command = open_file)
filemenu.add_separator()
filemenu.add_command(label='Exit', command = Exit)
root.config(menu=menubar)
open_file = Button(root, width = 6, height = 1, text = 'Mood',fg='Orange', bg='black')
open_file.grid(row=0, column=3)
play_button = Button(root, width = 5, height = 1, text='Play', fg='Orange', command = play, bg="black")
play_button.grid(row=0, column=0, sticky = W)
stop_button = Button(root, width = 5, height = 1, text='Stop',fg='Orange', command = stop, bg="black")
stop_button.grid(row=0, column=1, sticky = W)
pause_button = Button(root, width = 5, height = 1, text='Pause',fg='Orange', command = pause, bg="black")
pause_button.grid(row=0, column=2)
volume_slider = Scale(root, label='Volume', orient = 'horizontal', fg = 'Orange', command = vol, bg="black")
volume_slider.grid(row=0, column=4)
file_name_label = Label(root, font=('Comic Sans', 8), fg = 'Orange', wraplength = 300, textvariable=name, bg="black" )
file_name_label.grid(row=3, column=0, columnspan=8)

play_list_window = Toplevel(root, height = 150, width = 100)
play_list_window.title("Playlist")
play_list_display = Listbox(play_list_window, selectmode=EXTENDED, width = 50, bg="Dark Slate grey", fg="Orange")
play_list_display.bind("<Double-Button-1>", tune_changed)

play_list_display.pack()

play_list_window.mainloop()



root.mainloop()
4

2 回答 2

3

我查看了您的代码,因为我以前没有使用过 mp3play,并且认为它很有趣。

这是我更改的内容:

  1. 我将您的代码放在一个类中,以便在方法之间共享变量更容易和更清晰。它还消除了使用global. 通常会稍微清理一下代码,例如,分解过长的行。我尽量不要在没有必要的地方更改您的代码。
  2. 添加了trackLocations保留实际文件路径的列表。
  3. 添加了一行,在播放列表中双击加载新文件

这是结果,我希望它有帮助:

from Tkinter import *
import mp3play
import tkFileDialog
import Tkinter
import tkFont
import Tkinter as tk

class musicplay:
    def __init__(self):
        self.music = None
        self.play_list = []
        self.trackLocations = []

        self.root = tk.Tk()
        self.root.title("EmoPlayer")

        self.root.configure(background='black')
        self.root.geometry('300x100+750+300')

        self.filename = Tkinter.StringVar()
        self.name = Tkinter.StringVar()
        self.play_list = Tkinter.StringVar()

        menubar = Menu(self.root)
        filemenu = Menu(menubar, tearoff=0, bg="black", fg="Orange")
        menubar.add_cascade(label='File', menu = filemenu)
        filemenu.add_command(label='Open', command = self.open_file)
        filemenu.add_separator()
        filemenu.add_command(label='Exit', command = self.Exit)
        self.root.config(menu=menubar)

        open_file = Button(self.root, width = 6, height = 1,
                           text = 'Mood',fg='Orange', bg='black')
        open_file.grid(row=0, column=3)

        play_button = Button(self.root, width = 5, height = 1, text='Play',
                             fg='Orange', command = self.play, bg="black")
        play_button.grid(row=0, column=0, sticky = W)

        stop_button = Button(self.root, width = 5, height = 1, text='Stop',
                             fg='Orange', command = self.stop, bg="black")
        stop_button.grid(row=0, column=1, sticky = W)

        pause_button = Button(self.root, width = 5, height = 1, text='Pause',
                              fg='Orange', command = self.pause, bg="black")
        pause_button.grid(row=0, column=2)

        self.volume_slider = Scale(self.root, label='Volume',
                              orient = 'horizontal', fg = 'Orange', 
                              command = self.vol, bg="black")
        self.volume_slider.grid(row=0, column=4)

        file_name_label = Label(self.root, font=('Comic Sans', 8),
                                fg = 'Orange', wraplength = 300,
                                textvariable=self.name, bg="black")
        file_name_label.grid(row=3, column=0, columnspan=8)

        play_list_window = Toplevel(self.root, height = 150, width = 100)
        play_list_window.title("Playlist")
        self.play_list_display = Listbox(play_list_window, selectmode=EXTENDED,
                                    width = 50, bg="Dark Slate grey",
                                    fg="Orange")
        self.play_list_display.bind("<Double-Button-1>", self.tune_changed)
        self.play_list_display.pack()
        play_list_window.mainloop()

        self.root.mainloop()        

    def open_file(self):                                
        """
        Opens a dialog box to open .mp3 filemusic,
        then sends filename to file_name_label.
        """
        self.filename.set(tkFileDialog.askopenfilename(
            defaultextension = ".mp3",
            filetypes=[("All Types", ".*"), ("MP3", ".mp3")]))
        self.playlist = self.filename.get()
        playlist_pieces = self.playlist.split("/")
        self.play_list.set (playlist_pieces[-1])
        playl = self.play_list.get()
        self.play_list_display.insert(END, playl)
        print self.filename.get()
        self.music = mp3play.load(self.filename.get())
        pieces = self.filename.get().split("/")
        self.trackLocations += [self.filename.get()]
        self.name.set(pieces[-1])

    def play(self):
        """Plays the .mp3 file"""
        self.music.play()

    def stop(self):
        """Stops the .mp3 file"""
        self.music.stop()                

    def pause(self):
        """Pauses or unpauses the .mp3 file"""
        if self.music.ispaused():
            self.music.unpause()
        else:
            self.music.pause()

    def vol(self, event):
        """Allows volume to be changed with the slider"""
        v = Scale.get(self.volume_slider)
        try:
            self.music.volume(v)
        except:
            pass

    def tune_changed(self, event):
        idx = event.widget.curselection()[0]
        self.music = mp3play.load(self.trackLocations[int(idx)])
        print ("Now playing %s" % event.widget.get(idx))

    def Exit(self):
        exit()

if __name__ == "__main__":
    musicplay()
于 2012-06-21T05:12:47.903 回答
0

ValueError: int() 基数为 10 的无效文字:“指定的设备未打开或未被 MCI 识别。”

经过长时间的谷歌搜索后,我也遇到了这个错误,我在一些德国论坛上发现了一些 c++ 人的帖子,他说这是因为带有女巫 MCI 的 ID3v2 标签有问题。

然后我使用 mutagen - Python 多媒体标记库,在播放之前从 mp3 中去除标记,从那以后我再也没有遇到过这个错误。此外,如果使用诱变剂,请确保 mp3 文件在剥离其标签之前具有写入权限。

于 2013-04-21T09:50:35.987 回答