aaronmatt
Posts: 36
Joined: Mon Dec 22, 2014 3:04 pm

python audio file

Sat Feb 21, 2015 11:21 am

i am writing a game on python but need to add music what is the code for it thanks :)

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: python audio file

Sat Feb 21, 2015 11:33 am

You can use the Pygame mixer library. ;)

Laurens

aaronmatt
Posts: 36
Joined: Mon Dec 22, 2014 3:04 pm

Re: python audio file

Sat Feb 21, 2015 11:44 am

how does it work

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: python audio file

Sat Feb 21, 2015 12:17 pm

It should work like this:

Code: Select all

import pygame

pygame.mixer.init()
pygame.mixer.music.load("song.mp3")	#Open the mp3 file
pygame.mixer.music.set_volume(1.0)	#Volume at max between 0.0 and 1.0
pygame.mixer.music.play()				#Start the track
Laurens

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: python audio file

Sat Feb 21, 2015 6:32 pm

Please note that mp3 support on pygame is limited. You may want to convert your files to ogg.

You should also familiarise yourself with the pygame documentation as it explains what methods are available to you: http://www.pygame.org/docs/ref/music.html
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”