05_audioPlayer.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/python
  2. #version : 2023.12.31
  3. #language : ch
  4. import os
  5. from maix import display
  6. from maix import image
  7. from maix import camera
  8. import time
  9. import pyaudio
  10. import wave
  11. import sys
  12. sys.path.append('/root/')
  13. from CocoPi import BUTTON
  14. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  15. key_A = BUTTON(14)
  16. key_B = BUTTON(8)
  17. key_C = BUTTON(13)
  18. key_D = BUTTON(7)
  19. camera.camera.config(size=(240,320))
  20. buttonState=0
  21. newState=0
  22. oldState=0
  23. def buttonDetect():
  24. global buttonState,newState,oldState,key_C
  25. newState=key_B.is_pressed()
  26. if newState == True and oldState == False:
  27. buttonState=1
  28. elif newState == False and oldState == True:
  29. buttonState=2
  30. else:
  31. buttonState=0
  32. oldState=newState
  33. ###需要查询的文件夹目录及文件格式后缀###
  34. def getWavFileList(filePath="/root/user/audio/",selectFile=".wav"):
  35. import os
  36. FileList=[]
  37. aimFilePath=filePath
  38. aimFile=selectFile
  39. aimFileLenth=len(selectFile)*(-1)
  40. for root,dirs,files in os.walk(aimFilePath):
  41. for name in files:
  42. if str(name)[aimFileLenth:]==aimFile:
  43. FileList.append(name)
  44. return FileList
  45. wavShow=getWavFileList()
  46. print(wavShow)
  47. audioNum=len(wavShow)
  48. audioSelect=0
  49. playState=0
  50. playRunState=0
  51. canvas = image.new(size = (320, 240))
  52. while True:
  53. if audioNum==0:
  54. canvas.clear()
  55. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8)
  56. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8)
  57. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  58. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1)
  59. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1)
  60. canvas.draw_string(4, 140, "未找到WAV格式音频文件!", 1, color=(0, 255, 0))
  61. display.show(canvas)
  62. else:
  63. if key_C.is_pressed():
  64. while key_C.is_pressed():
  65. time.sleep(0.001)
  66. audioSelect=audioSelect-1
  67. elif key_D.is_pressed():
  68. while key_D.is_pressed():
  69. time.sleep(0.001)
  70. audioSelect=audioSelect+1
  71. else:
  72. buttonDetect()
  73. if buttonState==2:
  74. playState=1-playState
  75. print("playState",playState)
  76. if audioSelect>audioNum-1:
  77. audioSelect=0
  78. elif audioSelect<0:
  79. audioSelect=audioNum-1
  80. else:
  81. pass
  82. if playState==1:
  83. if playRunState==0:
  84. canvas.clear()
  85. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8)
  86. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8)
  87. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  88. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1)
  89. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1)
  90. canvas.draw_string(4, 80, "音频状态:正在播放!", 1, color=(0, 255, 0))
  91. canvas.draw_image((image.open("/root/preset/img/stop24.png")).rotate(0, adjust=0),4,216,alpha=1)
  92. canvas.draw_string(104, 218, wavShow[audioSelect], 1, color=(0, 255, 0))
  93. display.show(canvas)
  94. CHUNK = 1024
  95. wf = wave.open(r"/root/user/audio/"+wavShow[audioSelect], 'rb')
  96. p = pyaudio.PyAudio()
  97. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True)
  98. data = wf.readframes(CHUNK)
  99. playRunState=1
  100. elif playRunState==1:
  101. if len(data) > 0:
  102. stream.write(data)
  103. data = wf.readframes(CHUNK)
  104. else:
  105. stream.stop_stream()
  106. stream.close()
  107. p.terminate()
  108. canvas.clear()
  109. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8)
  110. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8)
  111. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  112. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1)
  113. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1)
  114. canvas.draw_string(4, 80, "音频状态:播放完成!", 1, color=(0, 255, 0))
  115. canvas.draw_image((image.open("/root/preset/img/play24.png")).rotate(0, adjust=0),4,216,alpha=1)
  116. canvas.draw_string(104, 218, wavShow[audioSelect], 1, color=(0, 255, 0))
  117. display.show(canvas)
  118. playRunState=0
  119. playState=0
  120. time.sleep(0.5)
  121. else:
  122. pass
  123. elif playState==0:
  124. if playRunState==0:
  125. canvas.clear()
  126. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8)
  127. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8)
  128. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  129. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1)
  130. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1)
  131. canvas.draw_string(4, 80, "音频状态:等待播放!", 1, color=(0, 255, 0))
  132. canvas.draw_image((image.open("/root/preset/img/play24.png")).rotate(0, adjust=0),4,216,alpha=1)
  133. canvas.draw_string(104, 218, wavShow[audioSelect], 1, color=(0, 255, 0))
  134. display.show(canvas)
  135. playRunState=0
  136. elif playRunState==1:
  137. stream.stop_stream()
  138. stream.close()
  139. p.terminate()
  140. canvas.clear()
  141. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 0,alpha=0.8)
  142. canvas.draw_image(image.open("/root/preset/img/blackBlock_7f7f7f_320x24.jpg"), 0, 216,alpha=0.8)
  143. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  144. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(0, adjust=0),6,4,alpha=1)
  145. canvas.draw_image((image.open("/root/preset/img/left_ffffff_16x16.png")).rotate(180, adjust=0),296,4,alpha=1)
  146. canvas.draw_string(4, 80, "音频状态:播放结束!", 1, color=(0, 255, 0))
  147. canvas.draw_image((image.open("/root/preset/img/play24.png")).rotate(0, adjust=0),4,216,alpha=1)
  148. canvas.draw_string(4, 100, "音频路径:"+wavShow[audioSelect], 1, color=(0, 255, 0))
  149. display.show(canvas)
  150. playRunState=0
  151. else:
  152. pass
  153. else:
  154. pass
  155. #canvas.clear()
  156. #canvas.draw_image((image.open("/root/user/audio/"+wavShowShow[audioSelect])),0,0)