- 论坛徽章:
- 0
|
看下这段代码,pygame写的一个循环播放背景音乐:- #-*- 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/任贤齐 - 风云决.mp3'
- p=player(song_path,end_flag)
- l=listen(song_path,end_flag)
- p.start()
- l.start()
复制代码 这样单独的测试没问题,可以循环播放歌曲.可当我在我的窗口模块使用,则不能循环播放了,求解答. |
|