b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

用Python为树莓派制作一个GPIO控制的音乐播放器

电脑杂谈  发布时间:2020-03-17 01:00:51  来源:网络整理

树莓派音乐播放器_树莓派 qq音乐_mp3剪辑器 mp3音乐剪辑器 音乐截取软件

话说最近真是干啥啥不行。

手机被收了,只能用树莓派听歌,不过我的派并没有配屏幕……所以最初的方式是:打开电脑->连上VNC->打开树莓派的浏览器访问网易云,虽然可唱,但是切歌、调音量都得通过手机调,很麻烦,而且开着Chromium树莓派发热恐怖。

手头上正好有红外接收头+,于是乎萌生了用控制树莓派的方式。先网上搜了一下有没有现成的轮子,结果“哇,LIRC一步到位”“哇,XBMC播放器”,然后我被诸多坑,被网上的这些半残教程坑、被硬件坑、被坑……经过一番艰苦奋斗,终于调好了,然而当我把调好的设备转移到书房的之后,红外接收头炸了……烧了,散发出一种刻骨铭心的糊味,熏出了我淡淡的悲哀。

悲愤的我再次刷回了raspbian,打开文本编辑器,自己来。

要不说我干啥啥不行呢,熬夜捣鼓出了100来行Python,还是在有pygame这个轮轴的前提下,实现了暂停和再次播放、音量调整,使用GPIO+微动开关控制。第二天上午加上了切歌功能。

首先是接线,线一大坨,不过很简单,就是接五个按键。如图(不含LCD):

树莓派音乐播放器_树莓派 qq音乐_mp3剪辑器 mp3音乐剪辑器 音乐截取软件

(本来自己摸索着连了一大坨很有成就感,画上去一看真TM简单)

以下是成品图,最终我添加了一块LCD1602树莓派音乐播放器,用来显示歌曲序号和音量:

然后是代码,用pygame库来推动音乐的播放和控制,多线程同时5个按键跟控制LCD,整体逻辑很简单,说真心的而是有许多不好把控的细节的,比如……额……控制上下曲的之后遏制数组溢出、确保LCD的文字正好占满全屏,以及一些反应播放状态变量方便提高新功能。

P.S.:关于监测GPIO口状态的方式,我用的是GPIO.event.detected(),这个会在后台持续,其实我终于用while轮询了,就没必要用这个,一般的GPIO.input()就可以了,懒得改了。

树莓派音乐播放器_mp3剪辑器 mp3音乐剪辑器 音乐截取软件_树莓派 qq音乐

添加音乐应该先执行scan.py生成目录。

main.py:

#coding=utf-8importRPi。GPIOasGPIO,time,pygame,threading,os,json#importLCD1602Modulefromlcd_i2cimport*lcd_string("Loading。。。",LCD_LINE_1)#GPIOsetup#part1GPIO。setmode(GPIO。BCM)#Pin2use[volumeup,volumedown,play/pause,next,previous]pinList=[17,27,18,22,23]pinVolume=pinList[0:2]GPIO。setup(pinList,GPIO。IN)forpinSingleinpinList:GPIO。add_event_detect(pinSingle,GPIO。RISING)pinPlay=pinList[2]pinNext=pinList[3]pinPrev=pinList[4]#Defaultvolumevalue(0。0~1。0)volume=0。50000000statelist=[0,0]#MusicPlayingwithopen('index。

list','r')asindex:filelist=json。loads(index。read())print(filelist)#filelist=['菅原纱由理(THE\xa0SxPLAY)-Guardian。mp3','不才-一身诗意千寻瀑。mp3','Riin-Alicegoodnight。mp3','林和夜-相聚万年树。mp3','苏尚卿-雪年轮(颜如玉ver)。mp3','菅原纱由理(THE\xa0SxPLAY)-キミが残した世界で。mp3','匀子-铃舟。mp3','花粥-一腔诗意喂了猫。mp3','Minnutes-ICan。mp3','绯村柯北,灰老板-东流。mp3','Cranky-LaPromesse。mp3','关晓彤-音梦。mp3','刘惜君-我很快乐。mp3','张恋歌-不忘(完整版)。mp3','呦猫UNEKO-梦回还。mp3','冥月,Mario-若当来世。mp3','林和夜-此彼绘卷。mp3']lens=len(filelist)filePlaying=0pygame。mixer。init()track=pygame。mixer。music。load(filelist[filePlaying])pygame。

mixer。music。set_volume(volume)pygame。mixer。music。play()pause=0#mainfunctiondefplaypause():globalpausewhileTrue:whileGPIO。event_detected(pinPlay):whileGPIO。event_detected(pinPlay)==0:ifpause==0:print('pausemusic')pygame。mixer。music。pause()pause=1else:print('resumemusic')pygame。mixer。music。unpause()pause=0breaktime。sleep(1)time。sleep(1)defvolume():#volumeglobalvolumevolume=0。5whileTrue:i=0forpinSingleinpinVolume:statelist[i]=GPIO。event_detected(pinSingle)i=i+1#print('Volumekeysstate',statelist)ifstatelist[0]==True:print('volumeup')volume=volume+0。

1ifvolume>1。0:volume=1。0print('volumevalue:',volume)ifstatelist[1]==True:print('volumedown')volume=volume-0。1ifvolume<0。0:volume=0。0print('volumevalue:',str(round(volume*10)))pygame。mixer。music。set_volume(volume)time。sleep(0。5)#自动切歌手动切歌defswitch():globalfilePlaying,track,pausewhileTrue:filePlayingchk=filePlayingifpygame。mixer。music。get_busy()==0:filePlaying=filePlaying+1else:ifGPIO。event_detected(pinNext)==1:whileGPIO。event_detected(pinNext)==0:filePlaying=filePlaying+1breakifGPIO。event_detected(pinPrev)==1:whileGPIO。

mp3剪辑器 mp3音乐剪辑器 音乐截取软件_树莓派 qq音乐_树莓派音乐播放器

event_detected(pinPrev)==0:filePlaying=filePlaying-1breakiffilePlaying<0:filePlaying=lens-1eliffilePlaying>lens-1:filePlaying=0iffilePlayingchk!=filePlaying:track=pygame。mixer。music。load(filelist[filePlaying])pygame。mixer。music。play()pause=0print(filePlaying+1,filelist[filePlaying])time。sleep(0。5)#LCD1602ModuledefdisplayContent():#MainprogramblockglobalfilePlaying,pause,volume,lens#Initialisedisplaylcd_init()whileTrue:#Sendsometestfigure=len(str(lens))+len(str(filePlaying+1))ifpause==0:playState="Playing"dots=8-figurewhiledots>0:playState=playState+"。

"dots=dots-1else:playState="Paused"dots=10-figurewhiledots>1:playState=playState+"。"dots=dots-1ifint(round(volume,1)*10)==10:dots2="。。。。。"else:dots2="。。。。。。"lcd_string(playState+str(filePlaying+1)+"/"+str(lens),LCD_LINE_1)lcd_string("Volume"+dots2+str(round(volume*10))+"/"+"10",LCD_LINE_2)time。sleep(1)defdisplayModule():whileTrue:if__name__=='__main__':try:displayContent()exceptKeyboardInterrupt:passfinally:lcd_byte(0x01,LCD_CMD)#Muti-threadsthreads=[]t1=threading。Thread(target=playpause)threads。

append(t1)t2=threading。Thread(target=volume)threads。append(t2)t3=threading。Thread(target=switch)threads。append(t3)t4=threading。Thread(target=displayModule)threads。append(t4)print('Wait。。。。。。')print(filePlaying+1,filelist[filePlaying])if__name__=='__main__':fortinthreads:t。setDaemon(True)t。start()t。join()

scan.py:

这两个文件还可以再整合,另外这种还是必须在SSH上自动运行的,可以添加一个开机启动服务。

LCD1602的操作库树莓派音乐播放器,会在main.py里导入:

树莓派 qq音乐_树莓派音乐播放器_mp3剪辑器 mp3音乐剪辑器 音乐截取软件

#!/usr/bin/python#coding=utf-8#--------------------------------------#___________#/_\/_\(_)__/______#/,_/___//\\/_\////#/_/|_/_//_/___/。__/\_,/#/_//___/##lcd_i2c。py#LCDtestscriptusingI2Cbackpack。#Supports16x2and20x4screens。##Author:MattHawkins#Date:20/09/2015####Copyright2015MattHawkins##Thisprogramisfreesoftware:youcanredistributeitand/ormodify#itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby#theFreeSoftwareFoundation,eitherversion3oftheLicense,or#(atyouroption)anylaterversion。##Thisprogramisdistributedinthehopethatitwillbeuseful,#butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof#MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-144409-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...