{"id":2385,"date":"2018-08-29T22:24:31","date_gmt":"2018-08-30T03:24:31","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2385"},"modified":"2019-06-10T11:25:58","modified_gmt":"2019-06-10T16:25:58","slug":"python-how-to-play-a-video-with-sound-in-pygame","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/08\/29\/python-how-to-play-a-video-with-sound-in-pygame\/","title":{"rendered":"Python: How to play a video (with sound) in Pygame"},"content":{"rendered":"<p>When creating a game in Pygame, the trick to getting a video with sound to play is to place <code>pygame.mixer.quit()<\/code> right after <code>pygame.init()<\/code>. This is because <code>pygame.mixer<\/code> doesn&#8217;t play well with <code>pygame.movie<\/code>. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">pygame.init()  \npygame.mixer.quit()<\/pre>\n<p>Here is what that looks like in the context of a larger block of code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import pygame  \n\nFPS = 60  \n\npygame.init()  \npygame.mixer.quit()  \nclock = pygame.time.Clock()  \nmovie = pygame.movie.Movie(r'C:\\Python27\\video\\test-mpeg.mpg')  \nscreen = pygame.display.set_mode(movie.get_size())  \nmovie_screen = pygame.Surface(movie.get_size()).convert()  \n\nmovie.set_display(movie_screen)  \nmovie.play()  \n\nplaying = True  \nwhile playing:  \nfor event in pygame.event.get():  \n if event.type == pygame.QUIT:  \n   movie.stop()  \n   playing = False  \n\nscreen.blit(movie_screen,(0,0))  \npygame.display.update()  \nclock.tick(FPS)  \n\npygame.quit()<\/pre>\n<p>Here is what the Pygame documentation says about <code>pygame.mixer.quit()<\/code>:<\/p>\n<blockquote><p>pygame.mixer.quit()<br \/>\nuninitialize the mixer<br \/>\nquit() -&gt; None<\/p>\n<p>This will uninitialize pygame.mixer pygame module for loading and playing sounds. All playback will stop and any loaded Sound objects may not be compatible with the mixer if it is reinitialized later.<\/p><\/blockquote>\n<p>More information is available in the Pygame documentation:<br \/>\n<a href=\"https:\/\/www.pygame.org\/docs\/ref\/mixer.html\">https:\/\/www.pygame.org\/docs\/ref\/mixer.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating a game in Pygame, the trick to getting a video with sound to play is to place pygame.mixer.quit() right after pygame.init(). This is because pygame.mixer doesn&#8217;t play well with pygame.movie. For example: pygame.init() pygame.mixer.quit() Here is what that looks like in the context of a larger block of code: import pygame FPS = &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/08\/29\/python-how-to-play-a-video-with-sound-in-pygame\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python: How to play a video (with sound) in Pygame<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[178,4],"class_list":["post-2385","post","type-post","status-publish","format-standard","hentry","category-python-language","tag-pygame","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2385"}],"version-history":[{"count":4,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2385\/revisions"}],"predecessor-version":[{"id":2746,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2385\/revisions\/2746"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}