一款簡易的音樂播放器

pip install pygame
pygame是跨平臺Python模組，專為電子遊戲設計，包含影象、聲音。
我這裡主要用到了pygame來播放音樂。pygame播放音樂有兩個方法，比如
music_one = pygame.mixer.Sound("test.mp3")
music_one.set_volume(0.05)
music_one.play()

pygame.mixer.music.load('test.mp3')
pygame.mixer.music.set_volume(0.05)
pygame.mixer.music.play()
單就實現效果而言，上面兩種方式是一樣的，細微的差別在於，pygame.mixer.Sound()
有返回值而pygame.mixer.music.load()沒有，假如我們在一個程式中需要在不同場景播放不同音樂，甚至可能是同時播放的，就不能用pygame.mixer.music.load()，因為它類似於一個全域性變數（這或許也是它不用返回的原因吧），後一次載入會覆蓋前一次載入，所以它適合播放背景音樂。而pygame.mixer.Sound()有返回值，我們可以通過把它賦值給一個變數，在需要播放音樂的場景使用變數名.play()就能立刻播放。
有關pygame更多的使用，比如play()函式的引數設定，請參考pygame官方教程

pip install wxPython
python的gui程式設計比較基礎的庫，就不多說了。

Source codes

# -*- coding: utf-8 -*-
# author:           inpurer(月小水長)
# pc_type           lenovo
# create_date:      2018/12/1
# file_name:        test.py
# description:      月小水長，熱血未涼

import os
import pygame
import random
import wx

musicUrlList = []

#載入工作目錄下的所有.mp3檔案
def musicUrlLoader():
   fileList = os.listdir(".")
   for filename in fileList:
      if filename.endswith(".mp3"):
         print("找到音訊檔案",filename)
         musicUrlList.append(filename)

class MyMusicPlayer(wx.Frame):
   def __init__(self,superion):
      wx.Frame.__init__(self,parent = superion, title = 'Xinspurer Player',size = (400,300))

      musicUrlLoader()

      MainPanel = wx.Panel(self)
      MainPanel.SetBackgroundColour('pink')

      self.ShowInfoText = wx.StaticText(parent = MainPanel, label = '播放未開始', pos = (100,100)
                                ,size = (185,25),style = wx.ALIGN_CENTER_VERTICAL)
      self.ShowInfoText.SetBackgroundColour('white')

      self.isPaused = False   #是否被暫停
      self.StartPlayButton = wx.Button(parent = MainPanel, label = '隨機播放', pos = (100,150))
      self.Bind(wx.EVT_BUTTON, self.OnStartClicked, self.StartPlayButton)

      self.PauseOrContinueButton = wx.Button(parent = MainPanel, label = '暫停播放', pos = (200,150))
      self.Bind(wx.EVT_BUTTON, self.OnPauseOrContinueClicked, self.PauseOrContinueButton)
      self.PauseOrContinueButton.Enable(False)

      pygame.mixer.init()



   def OnStartClicked(self,event):
      self.isPaused = False
      self.PauseOrContinueButton.Enable(True)

      self.willPlayMusic = random.choice(musicUrlList)
      pygame.mixer.music.load(self.willPlayMusic.encode())
      pygame.mixer.music.play()

      self.ShowInfoText.SetLabel("當前播放:"+self.willPlayMusic)


   def OnPauseOrContinueClicked(self,event):
      if not self.isPaused:
         self.isPaused = True
         pygame.mixer.music.pause()
         self.PauseOrContinueButton.SetLabel('繼續播放')

         self.ShowInfoText.SetLabel('播放已暫停')
      else:
         self.isPaused = False
         pygame.mixer.music.unpause()
         self.PauseOrContinueButton.SetLabel('暫停播放')

         self.ShowInfoText.SetLabel("當前播放:" + self.willPlayMusic)


if __name__ == "__main__":
   app = wx.App()
   myMusicPlayer = MyMusicPlayer(None)
   myMusicPlayer.Show()
   app.MainLoop()
需要注意的是，執行程式碼，需要在當前目錄上放幾首.mp3歌曲。

後記
所有程式碼在github有最新更新:PythonLearning
