Python实现音乐播放器

Python实现音乐播放器

/*
这是一个无界面的音乐播放器实现的mini project,
用来学习python自定结构体相关的一些知识,按文件结构创建文件并将代码复制进去即可运行
*/
功能实现实例

  • 文件结构

  • 主文件(main.py)

import library.manager as lm
from library.manager import c, Find
from mp.play import Play

ID=0
def libcommands(tokens):
	
	def listmusic():
		for i in range(len(c)):
			# print(len(c))
			e = lm.Get(i)
			print(i, ":", e.Name, e.Artist, e.Source, e.Type)
			i+=1

	def Addmusic():
		global ID
		# print('ID: '+ str(ID))
		if len(tokens) == 6:
			lm.Add([ID, tokens[2], tokens[3], tokens[4], tokens[5]])
			# print('Name: '+c[ID].Name)
			ID+=1
		else:
			print('Wrong CMD')
			print('USACMD: lib add <name><artist><source><type>')

	def removemusic():
		if len(tokens) == 3:
			lm.RemoveByName(tokens[2])
		else:
			print('Wrong CMD')
			print('USAGE: lib remove <name>')

	funMusic={"list": listmusic, "add": Addmusic, "remove": removemusic}
	if tokens[1] not in funMusic:
		print("Unrecognied lib commond: " + tokens[1])
	else:
		funMusic[tokens[1]]()
def Playcommand(tokens):
	if len(tokens) == 2:
		e = Find(tokens[1])
		if e:
			Play(e.Name, e.Type)
		else:
			print("The Music %s does not exist" %tokens[1])
	else:
		print("USAGE: play <name>")



def main():
	print('''
		Enter following commands to control the player:
		lib list -- View the existing music lib
		lib add <name><artist><source><type> -- Add a music to the music lib
		lib remove <name> -- Remove the specified music from the lib
		play <name> -- Play the specified music
		''')

	while True:
		print('Enter a CMD-->', end='')
		line = input()
		if line in 'qe':
			break
		tokens = line.split(' ')
		if tokens[0] == "lib":
			libcommands(tokens)
		elif tokens[0] == "play":
			Playcommand(tokens)
		else:
			print("Unrecognized command: " + tokens[0])
if __name__ == '__main__':
	main()
  • 功能实现(libaray-> manager.py)
class MusicEntry(object):
	"""docstring for MusicEntry"""
	def __init__(self):
		self.Id = 'test'
		self.Name = 'QiLixiang'
		self.Artist = 'Zhou'
		self.Source = 'www.qqmusic.com'
		self.Type = 'pop<-TEST->'

musics = MusicEntry()
c = ([musics])


# def Len():
# 	return len(musics.Id)

def Get(index):
	if index < 0 | index >= len(c):
		return str(c[index].Id) + 'Index out of range.'
	return c[index] 
def Find(name):
	# print(len(musics.Id))
	for i in c:
		if i.Name == name:
			return i		

def Add(music):
	# print('music[0]: ' + str(music[0]))
	e = Find(music[1])
	if e:
		print("The music %s already exist" %e.Name)
	else:
		c.append(MusicEntry())
		c[music[0]].Id=music[0]
		c[music[0]].Name=music[1]
		c[music[0]].Artist=music[2]
		c[music[0]].Source=music[3]
		c[music[0]].Type=music[4]
		print("Add music successful")
		# c.append(c[music[0]])		
	#每次add一组音乐添加新的结构体(有待改善)目的是增加c的长度

def RemoveByName(name):
	removedMusic = Find(name)
	try:
		
		if removedMusic:
			del c[removedMusic.Id]
			print("Remove %s successful !" %name)
			
		else:
			print("Want to delete the song does not exist")
			
			# c.append(MusicEntry())

	except Exception as e:
		print("test song cannot be removed !")
		
	
  • 播放文件(mp->play.py)
from mp.mp3 import Playmp3
from mp.WAV import Playwav
def Play(name, mtype):
	def MP3():
		Playmp3(name)
	def WAV():
		Playwav(name)
	funcType={'mp3':MP3, 'wav':WAV}
	mtype = mtype.lower()
	if mtype not in funcType:
		print("Unsupported music type: %s" %mtype)
	else:
		funcType[mtype]()
  • (mp->mp3.py)
import time
def Playmp3(name):
	#模拟播放mp3
	print("Playing MP3 music %s" %name)

	for i in range(1,10):
		time.sleep(1)
		print('.')

	print("Finished Playing %s" %name)

  • (mp->WAV.py)
import time
def Playwav(name):
	#模拟播放WAV
	print("Playing WAV music %s" %name)

	for i in range(1,10):
		time.sleep(1)
		print('.')

	print("Finished Playing %s" %name)
posted @ 2020-04-15 15:08  CodeMuscle  阅读(4632)  评论(0编辑  收藏  举报