您好,欢迎来到微智科技网。
搜索
您的当前位置:首页Python实现音乐播放器

Python实现音乐播放器

来源:微智科技网
Python实现⾳乐播放器

Python实现⾳乐播放器

/*

这是⼀个⽆界⾯的⾳乐播放器实现的mini project,

⽤来学习python⾃定结构体相关的⼀些知识,按⽂件结构创建⽂件并将代码复制进去即可运⾏*/

功能实现实例

⽂件结构

主⽂件(main.py)

import library.manager as lm

from library.manager import c, Findfrom 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, \":\ 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 ') def removemusic(): if len(tokens) == 3:

lm.RemoveByName(tokens[2]) else:

print('Wrong CMD')

print('USAGE: lib remove ')

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 \")

def main(): print('''

Enter following commands to control the player: lib list -- View the existing music lib

lib add -- Add a music to the music lib lib remove -- Remove the specified music from the lib play -- 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 Playmp3from mp.WAV import Playwavdef 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)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务