簡體   English   中英

如何使用pygame在歌曲自動完成后播放文件中的下一首歌曲

[英]How to play the next song in file after the song is finished automatically using pygame

我正在使用 pygame 制作音樂播放器。 但是我無法理解如何在上一首歌曲完成后自動播放下一首歌曲。 我的程序首先讀取選定文件夾中的所有 mp3 文件。 你能告訴我怎么做嗎?

import tkinter.filedialog as filedialog
import os
import pygame

directory = filedialog.askdirectory()
os.chdir(directory)
song_list = []
for file in os.listdir(directory):
    if file.endswith('.mp3'):
         song_list.append(file)
pygame.mixer.init()
pygame.mixer.music.load(song_list[0])
pygame.mixer.music.play()

我想在自動完成第一首歌曲后播放下一首歌曲。

這些文件可以在初始 for 循環中播放,您不需要將它們放在列表中然后播放它們。

關於播放 mp3 文件,您需要等待音樂播放后再退出。 完成每個文件后,您可以以相同的方式開始播放下一個文件。

import tkinter.filedialog as filedialog
import os
import pygame

directory = filedialog.askdirectory()
print("Loding files from directory:", directory)
os.chdir(directory)
pygame.mixer.init()
for file in os.listdir(directory):
    if file.endswith('.mp3'):
        print("Playing file:", file)
        pygame.mixer.music.load(file)
        pygame.mixer.music.play()
        # Wait for the music to play before exiting 
        while pygame.mixer.music.get_busy():   
            pygame.time.Clock().tick(5)

您可以通過列表框選擇目錄中的任何歌曲,也可以播放。

如果您想一首播放所有歌曲,您可以添加另一個按鈕,例如“Play_all”,並通過@saloua 將代碼分配給該按鈕。

鈴聲文件夾放置在此“audio_player.py”所在的同一文件夾中。

#audio_player.py
import os
import tkinter as tk
import pygame

def songs_list():
    files = [file for file in os.listdir('ringtone')]
    List = []
    for file in files:
        List.append(str(file))
        #print(file)
    #print(List)
    return List

def play(song_name):

    song_name_label['text'] = "Now Playing: " + song_name
    pygame.mixer.init()
    pygame.mixer.music.load("./ringtone/" + song_name)
    print("Playing:", song_name)
    pygame.mixer.music.play()

window = tk.Tk()
window .title("Any Name")
Height = 720
Width = 1080

# define size of window
canvas = tk.Canvas(window, bg='#3cd1fa',  height=Height, width=Width)
canvas.pack()

# play button **************************************************
play_button_frame = tk.Frame(window)
play_button_frame.place(relx=0.40, rely=0.88, relheight=0.1, relwidth=0.10)

play_button = tk.Button(play_button_frame, text="Play", font=25, fg='#d1d1d1', bg='black',
                        command=lambda: play(listbox.get(listbox.curselection()))  )
play_button.place(relx=0.01, rely=0.005, relheight=0.98, relwidth=0.49)


#list box (playlist) *************************************************
listbox_frame = tk.Frame(window, bg='green')
listbox_frame.place(relx=0.7, rely=0.2, relheight=0.6, relwidth=0.29)

listbox = tk.Listbox(listbox_frame, bg="white", selectmode='ACTIVE')
listbox.place(relx=0.01, rely=0.01, relheight=0.98, relwidth=0.98)

# song name *****************************************************************
song_name_frame = tk.Frame(window, bg='white')
song_name_frame.place(relx=0.20, rely=0.1, relheight=0.05, relwidth=0.60)
song_name_label = tk.Label(song_name_frame,font=("times now roman", 10))
song_name_label.place(relx=0.0, rely=0, relheight=1, relwidth=1)

# PLaylist, to display song in the list
playlist = songs_list()
for item in playlist:
    listbox.insert(tk.END, item)
# auto selecting 1st element of list box
listbox.selection_set( first = 0 )

window.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM