【问题标题】:Playing videos one after the other using Pygame使用 Pygame 一个接一个地播放视频
【发布时间】:2014-12-08 16:00:54
【问题描述】:

我编写了一些 Python 代码,试图在 Pygame 中一个接一个地显示一个视频:

import pygame
import time

def playvid(vidfile, runtime, FPS):
    print("playing" + vidfile)
    playmovie = pygame.movie.Movie(vidfile)
    movie_screen = pygame.Surface(playmovie.get_size()).convert()
    playmovie.set_display(movie_screen)
    playmovie.play()
    thentime = time.time()
    playing = True

    while playing:
        screen.blit(movie_screen,(0,0))
        pygame.display.update()
        clock.tick(FPS)
        nowtime = time.time()
        #Play the video for 5s
        if nowtime - thentime > runtime + 1: playing = False
    print("done this one")

#FPS = 29.97
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((0, 0),pygame.FULLSCREEN)
run = True
while run:
    playvid('snowdone.mpg', 10, 29.97)
    playvid('snowdone1.mpg', 10, 29.97)
    run = False

pygame.quit()

播放第一个视频没问题,但是播放第二个视频会发出警告:

“运行时错误!

程序:C:\Python27\pythonw.exe

此应用程序已请求运行时以不寻常的方式终止它。 请联系应用程序的支持团队了解更多信息。”

两个视频都可以单独播放,所以这不是 mpg 问题。有什么建议吗?

【问题讨论】:

    标签: python video pygame


    【解决方案1】:

    我不知道为什么我建议的更改有效的具体原因,但显然它有效。

    按原样使用您的代码,我的第一部电影无论如何都不会运行超过 4-5 秒(它总是在大致相同的点停止),然后 pygame 窗口会崩溃。

    while playing 循环中添加一段简单的pygame.event.get 代码,但是,解决了所有问题

    while playing:
        for event in pygame.event.get():
            pass
        screen.blit(movie_screen,(0,0))
        pygame.display.update()
        clock.tick(FPS)
        nowtime = time.time()
        #Play the video for 5s
        if nowtime - thentime > runtime + 1: playing = False
    

    奇怪的是,即使在事件的循环中什么也不做,代码也会毫无问题地运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多