`
cakin24
  • 浏览: 1333659 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

pygame+threading编写音乐播放器

阅读更多

一 代码

  1. import os
  2. import tkinter
  3. import tkinter.filedialog
  4. import random
  5. import time
  6. import threading
  7. import pygame
  8. folder =''
  9. def play():
  10. #默认播放D:\music文件夹中所有mp3文件
  11. global folder
  12. musics =[folder+'\\'+music for music in os.listdir(folder) \
  13. if music.endswith(('.mp3','.wav','.ogg'))]
  14. total = len(musics)
  15. #初始化混音器设备
  16. pygame.mixer.init()
  17. while playing:
  18. ifnot pygame.mixer.music.get_busy():
  19. #随机播放一首歌曲
  20. nextMusic = random.choice(musics)
  21. pygame.mixer.music.load(nextMusic.encode())
  22. #播放一次
  23. pygame.mixer.music.play(1)
  24. musicName.set('playing....'+nextMusic)
  25. else:
  26. time.sleep(0.3)
  27. root = tkinter.Tk()
  28. root.title('音乐播放器v1.0---cakin24')
  29. root.geometry('280x70+400+300')
  30. root.resizable(False,False)
  31. #关闭程序时执行的代码
  32. def closeWindow():
  33. global playing
  34. playing =False
  35. try:
  36. pygame.mixer.music.stop()
  37. pygame.mixer.quit()
  38. except:
  39. pass
  40. root.destroy()
  41. root.protocol('WM_DELETE_WINDOW', closeWindow)
  42. pause_resume = tkinter.StringVar(root, value='NotSet')
  43. playing =False
  44. #播放按钮
  45. def buttonPlayClick():
  46. global folder
  47. ifnot folder:
  48. folder = tkinter.filedialog.askdirectory()
  49. ifnot folder:
  50. return
  51. global playing
  52. playing =True
  53. #创建一个线程来播放音乐
  54. t = threading.Thread(target=play)
  55. t.start()
  56. #根据情况禁用和启用相应的按钮
  57. buttonPlay['state']='disabled'
  58. buttonStop['state']='normal'
  59. buttonPause['state']='normal'
  60. buttonNext['state']='normal'
  61. pause_resume.set('Pause')
  62. buttonPlay = tkinter.Button(root, text='Play', command=buttonPlayClick)
  63. buttonPlay.place(x=20, y=10, width=50, height=20)
  64. #停止按钮
  65. def buttonStopClick():
  66. global playing
  67. playing =False
  68. pygame.mixer.music.stop()
  69. musicName.set('暂时没有播放音乐')
  70. buttonPlay['state']='normal'
  71. buttonStop['state']='disabled'
  72. buttonPause['state']='disabled'
  73. buttonStop = tkinter.Button(root, text='Stop', command=buttonStopClick)
  74. buttonStop.place(x=80, y=10, width=50, height=20)
  75. buttonStop['state']='disabled'
  76. #暂停与恢复,两个功能共用一个按钮
  77. def buttonPauseClick():
  78. global playing
  79. if pause_resume.get()=='Pause':
  80. #playing = False
  81. pygame.mixer.music.pause()
  82. pause_resume.set('Resume')
  83. elif pause_resume.get()=='Resume':
  84. #playing = True
  85. pygame.mixer.music.unpause()
  86. pause_resume.set('Pause')
  87. buttonPause = tkinter.Button(root, textvariable=pause_resume,
  88. command=buttonPauseClick)
  89. buttonPause.place(x=140, y=10, width=50, height=20)
  90. buttonPause['state']='disabled'
  91. #下一首音乐
  92. def buttonNextClick():
  93. global playing
  94. playing =False
  95. pygame.mixer.music.stop()
  96. pygame.mixer.quit()
  97. buttonPlayClick()
  98. buttonNext = tkinter.Button(root, text='Next', command=buttonNextClick)
  99. buttonNext.place(x=200, y=10, width=50, height=20)
  100. buttonNext['state']='disabled'
  101. musicName = tkinter.StringVar(root, value='暂时没有播放音乐...')
  102. labelName = tkinter.Label(root, textvariable=musicName)
  103. labelName.place(x=0, y=40, width=270, height=20)
  104. #启动消息循环
  105. root.mainloop()
二 运行结果

 
  • 大小: 3.7 KB
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics