#!/usr/bin/python #version : 2023.12.31 #language : ch import os from maix import display from maix import image from maix import camera import time import pyaudio import wave import sys sys.path.append('/root/') from CocoPi import BUTTON image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf") key_A = BUTTON(14) key_B = BUTTON(8) key_C = BUTTON(13) key_D = BUTTON(7) camera.camera.config(size=(240,320)) buttonState=0 newState=0 oldState=0 def buttonDetect(): global buttonState,newState,oldState,key_C newState=key_B.is_pressed() if newState == True and oldState == False: buttonState=1 elif newState == False and oldState == True: buttonState=2 else: buttonState=0 oldState=newState ###需要查询的文件夹目录及文件格式后缀### def getWavFileList(filePath="/root/user/audio/",selectFile=".wav"): import os FileList=[] aimFilePath=filePath aimFile=selectFile aimFileLenth=len(selectFile)*(-1) for root,dirs,files in os.walk(aimFilePath): for name in files: if str(name)[aimFileLenth:]==aimFile: FileList.append(name) return FileList wavShow=getWavFileList() print(wavShow) audioNum=len(wavShow) audioSelect=0 playState=0 playRunState=0 canvas = image.new(size = (320, 240)) while True: if audioNum==0: canvas.clear() canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8) canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8) canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1) canvas.draw_string(4, 140, "未找到WAV格式音频文件!", 1, color=(0, 255, 0)) display.show(canvas) else: if key_C.is_pressed(): while key_C.is_pressed(): time.sleep(0.001) audioSelect=audioSelect-1 elif key_D.is_pressed(): while key_D.is_pressed(): time.sleep(0.001) audioSelect=audioSelect+1 else: buttonDetect() if buttonState==2: playState=1-playState print("playState",playState) if audioSelect>audioNum-1: audioSelect=0 elif audioSelect<0: audioSelect=audioNum-1 else: pass if playState==1: if playRunState==0: canvas.clear() canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8) canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8) canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1) canvas.draw_string(4, 80, "音频状态:正在播放!", 1, color=(0, 255, 0)) canvas.draw_image((image.open("/root/preset/img/stop24.png")).rotate(0, adjust=0),4,216,alpha=1) canvas.draw_string(104, 218, wavShow[audioSelect], 1, color=(0, 255, 0)) display.show(canvas) CHUNK = 1024 wf = wave.open(r"/root/user/audio/"+wavShow[audioSelect], 'rb') p = pyaudio.PyAudio() stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True) data = wf.readframes(CHUNK) playRunState=1 elif playRunState==1: if len(data) > 0: stream.write(data) data = wf.readframes(CHUNK) else: stream.stop_stream() stream.close() p.terminate() canvas.clear() canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8) canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8) canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1) canvas.draw_string(4, 80, "音频状态:播放完成!", 1, color=(0, 255, 0)) canvas.draw_image((image.open("/root/preset/img/play24.png")).rotate(0, adjust=0),4,216,alpha=1) canvas.draw_string(104, 218, wavShow[audioSelect], 1, color=(0, 255, 0)) display.show(canvas) playRunState=0 playState=0 time.sleep(0.5) else: pass elif playState==0: if playRunState==0: canvas.clear() canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8) canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8) canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1) canvas.draw_string(4, 80, "音频状态:等待播放!", 1, color=(0, 255, 0)) canvas.draw_image((image.open("/root/preset/img/play24.png")).rotate(0, adjust=0),4,216,alpha=1) canvas.draw_string(104, 218, wavShow[audioSelect], 1, color=(0, 255, 0)) display.show(canvas) playRunState=0 elif playRunState==1: stream.stop_stream() stream.close() p.terminate() canvas.clear() canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8) canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8) canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1) canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1) canvas.draw_string(4, 80, "音频状态:播放结束!", 1, color=(0, 255, 0)) canvas.draw_image((image.open("/root/preset/img/play24.png")).rotate(0, adjust=0),4,216,alpha=1) canvas.draw_string(4, 100, "音频路径:"+wavShow[audioSelect], 1, color=(0, 255, 0)) display.show(canvas) playRunState=0 else: pass else: pass #canvas.clear() #canvas.draw_image((image.open("/root/user/audio/"+wavShowShow[audioSelect])),0,0)