- 論壇徽章:
- 0
|
看下這段代碼,pygame寫的一個循環(huán)播放背景音樂:- #-*- coding: utf-8 -*-
- #file:test.py
- #
- import threading
- import pygame
- pygame.mixer.init()
- class player(threading.Thread):
- def __init__(self,song_path,end_flag):
- threading.Thread.__init__(self)
- self.end_flag = end_flag
- self.song_path = song_path
- def run(self):
- pygame.mixer.music.load(self.song_path)
- pygame.mixer.music.play()
- while pygame.mixer.music.get_busy():
- continue
- self.end_flag.remove('play')
- self.end_flag.append('done')
- class listen(threading.Thread):
- def __init__(self,song_path,end_flag):
- threading.Thread.__init__(self)
- self.end_flag = end_flag
- self.song_path = song_path
- def run(self):
- while True:
- if self.end_flag[0] == 'done':
- self.end_flag.remove('done')
- self.end_flag.append('play')
- p = player(self.song_path,self.end_flag)
- p.setDaemon(True)
- p.start()
- while True:
- if p.isAlive():
- break
- else:
- p.start()
- end_flag=['play']
- song_path='/host/KuGou/任賢齊 - 風(fēng)云決.mp3'
- p=player(song_path,end_flag)
- l=listen(song_path,end_flag)
- p.start()
- l.start()
復(fù)制代碼 這樣單獨的測試沒問題,可以循環(huán)播放歌曲.可當(dāng)我在我的窗口模塊使用,則不能循環(huán)播放了,求解答. |
|