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