我用PyGame写了一个播放器,但是运行的时候一直黑屏,还无响应,等了很久依旧没有反应,有没有人知道怎么回事?
import pygame
import tkinter as tk
from tkinter import filedialog
import os
import threading
class PygamePlayer:
def __init__(self):
self.screen_size = (800, 600)
pygame.init()
pygame.display.set_caption('PygamePlayer')
self.screen = pygame.display.set_mode(self.screen_size, pygame.HWSURFACE | pygame.DOUBLEBUF)
self.video_surface = pygame.Surface((640, 480))
self.clock = pygame.time.Clock()
self.duration = 0
self.current_time = 0
self.paused = True
self.full_screen = False
self.loaded = False
self.volume = 0.5
self.media_type = None
def load_media(self, path):
try:
if os.path.isfile(path):
self.loaded = True
if path.endswith('.mp4') or path.endswith('.avi') or path.endswith('.mkv'):
self.media_type = 'video'
self.media = pygame.movie.Movie(path)
self.media.set_display(self.video_surface, (0, 0, 640, 480))
self.duration = self.media.get_length()
elif path.endswith('.mp3') or path.endswith('.wav'):
self.media_type = 'audio'
pygame.mixer.music.load(path)
self.duration = pygame.mixer.Sound(path).get_length() * 1000
except:
print('文件加载失败!')
self.choose_file()
def play(self):
if self.media_type == 'video':
self.media.play()
self.paused = False
elif self.media_type == 'audio':
pygame.mixer.music.set_volume(self.volume)
pygame.mixer.music.play()
self.paused = False
def pause(self):
if self.media_type == 'video':
self.media.stop()
self.paused = True
elif self.media_type == 'audio':
pygame.mixer.music.pause()
self.paused = True
def rewind(self):
if self.media_type == 'audio':
current_time = pygame.mixer.music.get_pos() - 10000
if current_time < 0:
current_time = 0
pygame.mixer.music.set_pos(current_time)
elif self.media_type == 'video':
current_time = self.media.get_time() - 10000
if current_time < 0:
current_time = 0
self.media.set_time(current_time)
def fast_forward(self):
if self.media_type == 'audio':
current_time = pygame.mixer.music.get_pos() + 10000
if current_time > self.duration:
current_time = self.duration
pygame.mixer.music.set_pos(current_time)
elif self.media_type == 'video':
current_time = self.media.get_time() + 10000
if current_time > self.duration:
current_time = self.duration
self.media.set_time(current_time)
def toggle_full_screen(self):
self.full_screen = not self.full_screen
if self.full_screen:
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN | pygame.DOUBLEBUF)
else:
self.screen = pygame.display.set_mode(self.screen_size)
def choose_file(self):
def choose_file_thread():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
self.load_media(file_path)
self.play()
if not self.loaded:
threading.Thread(target=choose_file_thread).start()
def manage_keys(self, keys):
if keys[pygame.K_SPACE]:
if self.paused:
self.play()
else:
self.pause()
if keys[pygame.K_RIGHT]:
self.fast_forward()
if keys[pygame.K_LEFT]:
self.rewind()
if keys[pygame.K_ESCAPE]:
self.toggle_full_screen()
if keys[pygame.K_q]:
self.quit()
def quit(self):
pygame.quit()
quit()
def run(self):
while True:
if self.loaded:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
if event.type == pygame.KEYDOWN:
self.manage_keys(pygame.key.get_pressed())
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.choose_file()
if self.media_type == 'audio':
self.current_time = pygame.mixer.music.get_pos()
elif self.media_type == 'video':
self.current_time = self.media.get_time()
if self.media.get_time() >= self.duration:
self.media.stop()
self.media.play()
if self.full_screen:
self.video_surface = pygame.transform.scale(self.video_surface, self.screen_size)
else:
self.video_surface = pygame.transform.scale(self.video_surface, (640, 480))
self.screen.blit(self.video_surface, (0, 0))
time_text = '%02d:%02d:%02d / %02d:%02d:%02d' % (
self.current_time // 3600000,
self.current_time // 60000 % 60,
self.current_time // 1000 % 60,
self.duration // 3600000,
self.duration // 60000 % 60,
self.duration // 1000 % 60
)
font = pygame.font.SysFont(None, 30)
time_surface = font.render(time_text, True, (255, 255, 255))
self.screen.blit(time_surface, (10, 10))
pygame.display.update()
self.clock.tick(60)
if __name__ == '__main__':
player = PygamePlayer()
player.run()
而且没有报错。
图片:
先创建对象,init 里 把self.loaded设为了False。
然后.run(),while True里,判断self.loaded是不是真,这里为假,就又跳回while True这一行然后循环了,,
参考链接:https://blog.csdn.net/weixin_42636075/article/details/130393820