安装
1 | pip install pygame |
测试1 单纯播放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # pip install pygame import pygame import time class Play_vioce: #def __init__(self,ShareImages): def __init__(self,file_name): self.file_name=file_name self.player=pygame.mixer#只初始化音频部分 self.player.init() pass def set_name(self,file_name): self.file_name=file_name self.track = self.player.music.load(self.file_name) def play(self): self.track = self.player.music.load(self.file_name) self.player.music.play() #time.sleep(10) #pygame.mixer.music.stop() def stop(self): #pygame.mixer.music.play() #time.sleep(10) self.player.music.stop() file_name= "temp.mp3" test1=Play_vioce(file_name) print( "play" ) time.sleep(3) test1.play() print( "stop" ) time.sleep(3) test1.stop() |
2 多进程控制播放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import time import pygame import os from mutagen.mp3 import MP3 from multiprocessing import Process,Manager def GET_mp3List(): #获取地址并拼接 path_mp3 = 'mp3/' mp3_list_Path = [] for i in os.listdir(path_mp3): s = os.path.join(path_mp3,i) mp3_list_Path.append(s) print (i) return mp3_list_Path def Mp3_paly_all(): mp3_list_Path = GET_mp3List() if len (mp3_list_Path) = = 0 : print ( "没有数据" ) return 0 for Path_i in mp3_list_Path: # 获取每一首歌的时长 audio = MP3(Path_i) print ( "歌曲时间" , int (audio.info)) pygame.mixer.init() path = Path_i pygame.mixer.music.load(path) pygame.mixer.music.play( 10 ) #播放载入的音乐。该函数立即返回,音乐播放在后台进行 time.sleep( int ( 10 )) pygame.mixer.music.pause() # 暂停 time.sleep( int ( 3 )) pygame.mixer.music.unpause() # 接着播放 #获取歌曲时间 time.sleep( int (audio.info.length)) def Mp3_paly_while(ShareImages,lock): pygame.mixer.init() while 1 : if ShareImages[ 9 ] = = 1 : print ( "mp3 stop" ) if ShareImages[ 6 ] = = "paly" : ShareImages[ 6 ] = "wait" path = ShareImages[ 7 ] pygame.mixer.music.load(path) print ( "开始播放歌曲" ,path) #audio = MP3(path) #print("歌曲时间",int(audio.info)) pygame.mixer.music.play() elif ShareImages[ 6 ] = = "stop" : ShareImages[ 6 ] = "wait" pygame.mixer.music.pause() # 暂停 time.sleep( 0.0001 ) ''' if __name__ == '__main__': ShareImages=Manager().dict() lock=Manager().Lock() ShareImages[6]="wait"# ,mp3 动作 ShareImages[7]="wait"# ,mp3 当前播放名字 ShareImages[9]=0#用于关闭所有进程 #1采集图像进程 t1=Process(target=Mp3_paly_while,args=(managerdata,sharelock)) t1.start() t1.deamon=True #伴随主进程关闭而关闭 while 1: pass ''' |
qt界面 mp3播放器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5 import QtWidgets, QtCore, QtGui,uic # pip install PyQt5 # pip install PyQt5-sip # pip install PyQtWebEngine --user #导入图像库 import cv2 import numpy as np import datetime #进程测试 #import multiprocessing import sys #线程测试 import threading from queue import Queue from multiprocessing import Process,Manager #视频采集和处理进程 from Api_GPS import * #串口进程 from Api_MP3 import * from Api_Weather import * import time """ time格式 '2018-01-03 16:26:23' """ def compare_time( self ,startTime,endTime): #now = datetime.datetime.now () d_start = datetime.datetime.strptime (startTime, '%Y-%m-%d %H:%M:%S' ) d_end = datetime.datetime.strptime (endTime, '%Y-%m-%d %H:%M:%S' ) diff = d_end - d_start diff_days = - diff.days #result = (d_start<=now) and (d_end>=now) #result = (d_start==d_end) #print(diff_days) return diff_days #result = compare_time('2018-01-03 16:26:23','2018-02-03 16:29:55') #print(result) #一个是这个类本身的,一个是这个类继承 class Example(QMainWindow): def __init__( self ,ShareImages,Lock): #类的初始化 super (Example, self ).__init__() uic.loadUi( 'UI.ui' , self ) #self.setupUi(self)#界面控件初始化 #获取共享内存,外部参数数据和数据锁 self .ShareImages = ShareImages self .Lock = Lock #相机线程初始化 self .UI_Int() self .MP3_INT() def MP3_INT( self ): self .mp3_list_Path = GET_mp3List() self .mp3_allnum = len ( self .mp3_list_Path) self .mp3_id = 0 self .plainTextEdit_mp3.clear() if self .mp3_allnum = = 0 : self .plainTextEdit_mp3.appendPlainText( "MP3目录里没有要播放的文件" ) ShareImages[ 7 ] = 'null' else : for mi in range ( 0 , self .mp3_allnum): self .plainTextEdit_mp3.appendPlainText( "ID:" + str (mi) + " Name: " + str ( self .mp3_list_Path[mi])) #audio = MP3(mi) #print(audio.info) ShareImages[ 7 ] = str ( self .mp3_list_Path[ self .mp3_id]) def UI_Int( self ): #self.dateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss") #按钮绑定函数 self .pushButton_begin.clicked.connect( self .callback_begin) #开始按键 self .pushButton_stop.clicked.connect( self .callback_stop) self .pushButton_up.clicked.connect( self .callback_up) self .pushButton_down.clicked.connect( self .callback_down) #定时器20毫秒更新画板 self .timer = QTimer( self ) self .timer.timeout.connect( self .GPS_SetImage) self .timer.start( 1000 ) #单位为毫秒 self .timer_weather = QTimer( self ) self .timer_weather.timeout.connect( self .callback_weather) self .timer_weather.start( 10000 ) #单位为毫秒 def callback_weather( self ): city_name_list = [ '重庆' ] # 获取以上五个城市天气数据 #for cn in city_name_list: city_code = search_city_code(city_name_list[ 0 ]) html_str = getHTMLtext(city_code) result = get_data(html_str) # 获得1-7天和当天的数据 self .plainTextEdit_weather.clear() msg_ = "所在位置:" + str ( city_name_list[ 0 ]) + " 城市编码:" + str (city_code) + " 未来7日天气" self .plainTextEdit_weather.appendPlainText(msg_) if len (result) = = 0 : self .plainTextEdit_weather.appendPlainText( "没有天气数据" ) else : for li in result: data = li[ 0 ] tianqi = li[ 1 ] tem_h = li[ 2 ] tem_l = li[ 3 ] msg = str (data) + " " + str (tianqi) + " " + str (tem_h) + " " + str (tem_l) + " " self .plainTextEdit_weather.appendPlainText(msg) pass def callback_begin( self ): print ( "开始播放" ) _translate = QtCore.QCoreApplication.translate if self .mp3_allnum = = 0 : print ( "没有数据" ) _translate = QtCore.QCoreApplication.translate self .label_msg.setText(_translate( "Form" , "曲目中没有数据要播放" )) else : self .label_msg.setText(_translate( "Form" , "开始播放" + self .ShareImages[ 7 ])) ShareImages[ 7 ] = str ( self .mp3_list_Path[ self .mp3_id]) self .ShareImages[ 6 ] = "paly" def callback_stop( self ): self .ShareImages[ 6 ] = "stop" print ( "停止播放" ) _translate = QtCore.QCoreApplication.translate self .label_msg.setText(_translate( "Form" , "停止播放" + self .ShareImages[ 7 ])) def callback_up( self ): print ( "上一曲" ) if self .mp3_allnum = = 0 : print ( "没有数据" ) _translate = QtCore.QCoreApplication.translate self .label_msg.setText(_translate( "Form" , "曲目中没有数据要播放" )) else : self .mp3_id = self .mp3_id - 1 if self .mp3_id< = 0 : self .mp3_id = 0 mp3_name = str ( self .mp3_list_Path[ self .mp3_id]) self .ShareImages[ 7 ] = mp3_name self .ShareImages[ 6 ] = 'paly' _translate = QtCore.QCoreApplication.translate self .label_msg.setText(_translate( "Form" , "上一曲" + self .ShareImages[ 7 ])) def callback_down( self ): print ( "下一曲" ) if self .mp3_allnum = = 0 : print ( "没有数据" ) _translate = QtCore.QCoreApplication.translate self .label_msg.setText(_translate( "Form" , "曲目中没有数据要播放" )) else : self .mp3_id = self .mp3_id + 1 if self .mp3_id> = self .mp3_allnum - 1 : self .mp3_id = self .mp3_allnum - 1 mp3_name = str ( self .mp3_list_Path[ self .mp3_id]) self .ShareImages[ 7 ] = mp3_name self .ShareImages[ 6 ] = 'paly' _translate = QtCore.QCoreApplication.translate self .label_msg.setText(_translate( "Form" , "下一曲" + self .ShareImages[ 7 ])) #配合定时器刷新图像显示 def GPS_SetImage( self ): now_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) #parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") _translate = QtCore.QCoreApplication.translate self .lineEditDate.setText(_translate( "Form" , now_time)) self .plainTextEdit_GPS.clear() self .plainTextEdit_GPS.appendPlainText( "经度:" + str ( self .ShareImages[ 2 ])) self .plainTextEdit_GPS.appendPlainText( "纬度:" + str ( self .ShareImages[ 3 ])) self .plainTextEdit_GPS.appendPlainText( "高度:" + str ( self .ShareImages[ 4 ])) self .plainTextEdit_GPS.appendPlainText( "时间:" + str ( self .ShareImages[ 5 ])) self .plainTextEdit_GPS.appendPlainText( "状态:" + str ( self .ShareImages[ 1 ])) def closeEvent( self , event): self .ShareImages[ 9 ] = 1 #必须通过共享内存数据,强制关闭采集进程 print ( "QT关闭窗口" ) if __name__ = = '__main__' : lock = Manager().Lock() #创建共享内存容器 ShareImages = Manager(). dict () #存str类型数据 ShareImages[ 1 ] = 0 #"是否可用" ShareImages[ 2 ] = 0.0 #"经度" ShareImages[ 3 ] = 0.0 #"纬度" ShareImages[ 4 ] = 0.0 #"高度" ShareImages[ 5 ] = 0 #"时间" ShareImages[ 6 ] = "wait" # ,mp3 动作 ShareImages[ 7 ] = "wait" # ,mp3 当前播放名字 ShareImages[ 9 ] = 0 #用于关闭所有进程 #3采集GPS p_gps = Process(target = gps_Get_data, args = (ShareImages,lock)) #开启进程 p_gps.deamon = True #伴随主进程关闭而关闭,但是有时候还是关闭不了,单独搞个标志位来控制 p_gps.start() #开始 #3采集MP3 p_MP3 = Process(target = Mp3_paly_while, args = (ShareImages,lock)) #开启进程 p_MP3.deamon = True #伴随主进程关闭而关闭,但是有时候还是关闭不了,单独搞个标志位来控制 p_MP3.start() #开始 #----------------------QT界面主进程-------------------------------- app = QApplication(sys.argv) ui = Example(ShareImages,lock) #ShareImages共享内存数据 lock 共享数据锁 ui.show() sys.exit(app.exec_()) |
3
附录
pygame.init() 进行全部模块的初始化,
pygame.mixer.init() 或者只初始化音频部分
pygame.mixer.music.load('xx.mp3') 使用文件名作为参数载入音乐 ,音乐可以是ogg、mp3等格式。载入的音乐不会全部放到内容中,而是以流的形式播放的,即在播放的时候才会一点点从文件中读取。
pygame.mixer.music.play()播放载入的音乐。该函数立即返回,音乐播放在后台进行。
play方法还可以使用两个参数
pygame.mixer.music.play(loops=0, start=0.0) loops和start分别代表重复的次数和开始播放的位置。
pygame.mixer.music.stop() 停止播放,
pygame.mixer.music.pause() 暂停播放。
pygame.mixer.music.unpause() 取消暂停。
pygame.mixer.music.fadeout(time) 用来进行淡出,在time毫秒的时间内音量由初始值渐变为0,最后停止播放。
pygame.mixer.music.set_volume(value) 来设置播放的音量,音量value的范围为0.0到1.0。
pygame.mixer.music.get_busy() 判断是否在播放音乐,返回1为正在播放。
pygame.mixer.music.set_endevent(pygame.USEREVENT + 1) 在音乐播放完成时,用事件的方式通知用户程序,设置当音乐播放完成时发送pygame.USEREVENT+1事件给用户程序。 pygame.mixer.music.queue(filename) 使用指定下一个要播放的音乐文件,当前的音乐播放完成后自动开始播放指定的下一个。一次只能指定一个等待播放的音乐文件。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2020-04-05 树莓派控制WS2812