此代码是上一期的改版
需要用到的Python库有
1.pygame
2.time
3.xmusic(我自己写的用来做音乐索引)
4.colorama(美观)
推荐使用pip安装
方法: pip install 库
1 # -* encoding:utf-8 *- 2 import xmusic 3 import colorama 4 from colorama import Fore 5 # 音乐库 6 # Music 7 while True: 8 colorama.init() 9 text = ''' 10 ----↓歌曲列表↓---- 11 12 1.02的爱恋 13 2.奇迹再现 14 3.花降 15 4.布鲁克的心跳 16 17 ''' 18 print(Fore.RED + text) 19 20 M_1 = {'02的爱恋'} 21 M_2 = {'奇迹再现'} 22 M_3 = {'花降'} 23 M_4 = {'布鲁克的心跳'} 24 # 要查的歌名 25 name_m = input('输入要搜索的歌名:') 26 request = input("点击任意键播放...") 27 print('——————————————————————————') 28 if name_m in M_1:xmusic.M_02deailian() 29 if name_m in M_2:xmusic.M_qijizaixian() 30 if name_m in M_3:xmusic.M_huajiang() 31 if name_m in M_4:xmusic.M_bulukedexintiao() 32 s = input('输入[y]继续听歌 其他退出:') 33 if s == 'y':print() 34 else:break
导入步骤:
1.下载xmusic(点击蓝色字体下载)
2.把下载好的文件放到Python目录里的Lib
运行效果:
xmusic库:代码如下
1 #奇迹再现 2 import pygame 3 import time 4 5 def M_qijizaixian(): 6 f = open('Musicqjzxqj.txt', 'r', encoding='UTF-8') 7 f = f.read() 8 print(f) 9 file = r'Musicqjzxqjzx.mp3' # mp3文件路径 10 pygame.mixer.init() 11 print("正在播放", file) 12 track = pygame.mixer.music.load(file) 13 pygame.mixer.music.play() 14 time.sleep(3 15 16 17 ) # 播放时间 18 pygame.mixer.music.stop() 19 return 20 21 #02的爱恋 22 def M_02deailian(): 23 f = open('Music\02\geci.txt', 'r', encoding='UTF-8') 24 f = f.read() 25 print(f) 26 file = r'Music 2 2.mp3' # mp3文件路径 27 pygame.mixer.init() 28 print("正在播放", file) 29 track = pygame.mixer.music.load(file) 30 pygame.mixer.music.play() 31 time.sleep(304) # 播放时间 32 pygame.mixer.music.stop() 33 #花降 34 def M_huajiang(): 35 f = open('Music\花降\hj.txt', 'r', encoding='UTF-8') 36 f = f.read() 37 print(f) 38 file = r'Music花降ナブナ - 花降らし.mp3' # mp3文件路径 39 pygame.mixer.init() 40 print("正在播放", file) 41 track = pygame.mixer.music.load(file) 42 pygame.mixer.music.play() 43 time.sleep(304) # 播放时间 44 pygame.mixer.music.stop() 45 46 # 47 def M_bulukedexintiao(): 48 f = open('Music\布鲁克\布鲁克的心跳.txt', 'r', encoding='UTF-8') 49 f = f.read() 50 print(f) 51 file = r'Music布鲁克海小贼 - 布鲁克的心跳(Cover:萧亚轩).mp3' # mp3文件路径 52 pygame.mixer.init() 53 print("正在播放", file) 54 track = pygame.mixer.music.load(file) 55 pygame.mixer.music.play() 56 time.sleep(130) # 播放时间 57 pygame.mixer.music.stop()
原创 不经允许禁止转载