70行python代码制作一款简易的音乐播放器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ygdxt/article/details/84679556

今天整理了以前的python作业代码,发现了一些有趣的小东西,比如下面这个,大概70行代码制作一款简易的音乐播放器。

install some packages

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
欢迎关注个人公众号: inspurer

猜你喜欢

转载自blog.csdn.net/ygdxt/article/details/84679556