Playing Video

From TRCCompSci - AQA Computer Science
Revision as of 10:59, 5 July 2018 by Admin (talk | contribs) (Created page with "=Setup= Make sure you have a working pygame program, after the pygame.init() add the following: <syntaxhighlight lang=python> FPS = 60 clock = pygame.time.Clock() movie = pyg...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Setup

Make sure you have a working pygame program, after the pygame.init() add the following:

FPS = 60
clock = pygame.time.Clock()
movie = pygame.movie.Movie('MELT.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)

The frame rate and the clock are important because the video lasts for a specific amount of time, if you don't control the frame rate it will play the video as quickly as possible.

Start & Playing the Movie

The movie can be started using the play() method, but remember it needs to be drawn to the screen. So we need to include some sort of game loop:

movie.play()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            movie.stop()
             pygame.quit()

    screen.blit(movie_screen,(0,0))
    pygame.display.update()
    clock.tick(FPS)

No Sound

If you get no sound, it is because the pygame.mixer is initialised. So you can quit the pygame.mixer after the pygame.init():

pygame.mixer.quit()