如何在 Pygame 中新增音樂播放列表?
Pygame 可以處理音訊,使其成為在我們的應用程式中建立和新增音樂播放列表的絕佳選擇。Pygame 通常是一個 Python 庫,廣泛用於建立遊戲和多媒體應用程式,例如音樂播放器。在本文中,我們將討論在 Pygame 中新增音樂播放列表的步驟,並建立一個簡單的程式,允許使用者播放和更改(上一首或下一首歌曲)播放列表中的歌曲。
Pygame 依賴於 SDL 庫(Simple DirectMedia Layer)進行音訊處理。因此,要使用 Pygame 的音訊功能,我們必須在我們的系統上安裝 SDL。我們可以從官方網站下載 SDL 庫,也可以透過我們系統的包管理器下載它。
如何在 Pygame 中新增音樂播放列表?
以下是我們將遵循的演算法,用於在 Pygame 中新增音樂播放列表:
演算法
步驟 1 - 匯入 Pygame 並初始化它。
在 Pygame 中新增音樂播放列表的最初步驟是匯入 Pygame 模組並初始化它。
步驟 2 - 載入音樂檔案
初始化 Pygame 模組後,我們現在可以載入我們想要播放的音樂檔案。Pygame 支援多種音訊格式,包括 WAV、MP3 和 OGG。我們將使用 pygame.mixer.music.load() 載入音樂檔案。
步驟 3 - 播放音樂
我們將使用 pygame.mixer.music.play() 函式播放音樂檔案。
步驟 4 - 建立音樂播放列表
我們已經瞭解瞭如何載入和播放音樂檔案,現在可以透過將音樂檔案的名稱儲存在列表中來建立音樂播放列表。
示例
import pygame # Initialize Pygame pygame.init() # Set up the screen (not needed for audio) screen = pygame.display.set_mode((640, 480)) # Create a list of music files to play music_files = ["song1.mp3", "song2.mp3", "song3.mp3"] # Load the first song in the list current_song_index = 0 pygame.mixer.music.load(music_files[current_song_index]) # Play the current song pygame.mixer.music.play() # Set up fonts for displaying song titles font = pygame.font.SysFont("Arial", 24) # Create buttons to skip to the next song and go back to the previous song next_button_rect = pygame.Rect(500, 200, 100, 50) next_button_text = font.render("Next", True, (255, 255, 255)) prev_button_rect = pygame.Rect(100, 200, 100, 50) prev_button_text = font.render("Prev", True, (255, 255, 255)) # Start the game loop while True: # Handle events (including button clicks) for event in pygame.event.get(): if event.type == pygame.QUIT: # User clicked the "X" button, exit the game pygame.quit() quit() elif event.type == pygame.MOUSEBUTTONDOWN: # Check if the user clicked one of the buttons if next_button_rect.collidepoint(event.pos): # User clicked the "next" button, go to the next song in the list current_song_index += 1 if current_song_index >= len(music_files): # If we've reached the end of the list, wrap around to the beginning current_song_index = 0 pygame.mixer.music.load(music_files[current_song_index]) pygame.mixer.music.play() elif prev_button_rect.collidepoint(event.pos): # User clicked the "prev" button, go to the previous song in the list current_song_index -= 1 if current_song_index < 0: # If we've reached the beginning of the list, wrap around to the end current_song_index = len(music_files) - 1 pygame.mixer.music.load(music_files[current_song_index]) pygame.mixer.music.play() # Draw the buttons and current song title on the screen screen.fill((0, 0, 0)) pygame.draw.rect(screen, (255, 0, 0), next_button_rect) pygame.draw.rect(screen, (255, 0, 0), prev_button_rect) screen.blit(next_button_text, next_button_rect) screen.blit(prev_button_text, prev_button_rect) current_song_title = font.render(music_files[current_song_index], True, (255, 255, 255)) screen.blit(current_song_title, (320 - current_song_title.get_width() // 2, 100 - current_song_title.get_height() // 2)) # Update the screen pygame.display.update()
上面的程式建立了一個音樂檔案列表:song1.mp3、song2.mp3、song3.mp3,您需要下載 mp3 檔案並將其替換為 song1、song2 和 song 3。它還建立了兩個按鈕,用於跳到下一首歌曲或返回上一首歌曲。當用戶點選其中一個按鈕時,程式將維護和更新當前歌曲索引,並載入和播放列表中的新歌曲,例如,如果使用者點選“下一首”按鈕,則程式將更新列表,並播放列表中的下一首歌曲。程式還將在螢幕上顯示當前正在播放的歌曲。
輸出
開啟命令提示符並貼上以下命令:
python music.py
Music 是檔名。在輸入上述命令後按 Enter 鍵。
螢幕上將出現以下螢幕,螢幕上有 2 個按鈕和當前歌曲的名稱。
結論
總之,名為 Pygame 的 Python 庫提供了一個簡單易用的介面,用於在我們的應用程式中新增音樂播放列表。透過載入和播放多個音訊檔案的能力,我們可以輕鬆地建立一個音樂播放列表,並且還可以新增控制元件來更改歌曲。憑藉一點創造力和 Pygame 知識,我們甚至可以新增其他一些功能,例如隨機播放按鈕或進度按鈕。總的來說,使用 Pygame 建立或新增音樂播放列表可以增強遊戲或其他多媒體應用程式的使用者體驗。