Pygame.mixer is a module for handling sounds, meaning "mixer". In the game, the sound processing generally includes making the sound and playing the sound two parts, here only learns to play the sound part.
1, Pygame.mixer Startup and initialization
Pygame.mixer.init (): Initialization of the mixer. In a program, when used, it is generally placed in the first few lines of code:
Copy Code code as follows:
Import Pygame
Pygame.init ()
Pygame.mixer.init ()
2. Play Audio clips wav files
When playing a sound fragment wav file, Pygame.mixer uses the sound object in the format:
Copy Code code as follows:
Soundwav=pygame.mixer.sound ("Filename.wav") #filename. wav filename
Soundwav.play ()
In the game, the following code is often used to complete:
Copy Code code as follows:
Import Pygame,sys
Pygame.init ()
Pygame.mixer.init ()
Screen=pygame.display.set_mode ([640,480])
Pygame.time.delay (1000) #等待1秒让mixer完成初始化
Soundwav=pygame.mixer.sound ("Filename.wav")
Soundwav.play ()
While 1:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()
3, play MP3, WMA, OGG music files
This is just a mp3 file, and if it's not a Windows system, try the ogg file. The main use of music in Pygame.mixer, the Main method is:
Copy Code code as follows:
Pygame.mixer.music.load ("Filename.mp3")
Pygame.mixer.music.play (n,start,stop) #第一个参数为播放次数, if 1 indicates a looping play, the omitted representation only plays 1 times. The second and third arguments represent the starting and ending positions of the playback.
The complete code is:
Copy Code code as follows:
Import Pygame,sys
Pygame.init ()
Pygame.mixer.init ()
Screen=pygame.display.set_mode ([640,480])
Pygame.time.delay (1000)
Pygame.mixer.music.load ("Filename.mp3")
Pygame.mixer.music.play ()
While 1:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()
When the program is running, once you start playing the music, you'll continue to do the next thing, that is, if we're going to play two songs,
Copy Code code as follows:
Pygame.mixer.music.load ("Filename1.mp3")
Pygame.mixer.music.play ()
Pygame.mixer.music.load ("Filename2.mp3")
Pygame.mixer.music.play ()
After this code runs, there will be two songs playing at the same time. In practice, we often need to play a song and play another one, so we need to use the Pygame.mixer.music.get_busy () function to determine if the function return value is true so that the current busy state, otherwise return False. For example:
Copy Code code as follows:
Import Pygame,sys
Pygame.init ()
Pygame.mixer.init ()
Screen=pygame.display.set_mode ([640,480])
Pygame.time.delay (1000)
Pygame.mixer.music.load ("Filename.mp3")
Pygame.mixer.music.play ()
Soundwav=pygame.mixer.sound ("Filename.wav")
While 1:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()
If not pygame.mixer.music.get_busy ():
Soundwav.play ()
Pygame.time.delay (5000) #等待5秒让filename. WAV Playback End
Sys.exit ()
4. Control Volume
Pygame.mixer.music.set_volume () is used to control the volume, with a value range of 0-1.0 floating-point numbers. 0 is the minimum value and 1 is the maximum value.
5, the production of MP3 players encountered problems
Python+wxpython+pygame can easily make a MP3 player, but there are two problems in production:
One is the Chinese song name display, through the search has been resolved, plus # encoding= ' GBK '
Second, pygame in the play MP3 file, found that not all MP3 files have playback, often appear to play sound incorrect situation, sometimes just miscellaneous noise. After interacting with the すeasyげ in the Python Learning Exchange Group (2), すeasyげ proposed a bit-speed 128kbps MP3 file to play, but after several 128kbps MP3 tests from the Internet, it was found that the abnormal playback still existed. I do not know which Daniel can solve this problem, I hope to tell the solution, thank you.
So, if you're going to write a mp3 player, Python can do a lot of MP3 playback, here, I think we can consider using the Mp3play module, more convenient and simple. The current version of Mp3play 0.1.15, the specific download address for http://pypi.python.org/pypi/mp3play/. Here is a brief introduction to its main methods:
Play (N,STARMS,ENDMS): Plays, the first parameter represents the number of times, and the second to third parameter indicates the starting and ending position of the playback, in milliseconds.
Stop (): Stopped.
Pause (), unpause (): Pause and start.
IsPlaying (): Determines if playback is in progress, and returns true to indicate that a song is playing.
Ispaused (): Determines whether a pause is paused and, if returned to true, indicates a pause.
Seconds (): Returns the MP3 file for a total of seconds, note that the unit here is seconds.
Milliseconds (): Returns the number of milliseconds for the MP3 file, noting that the units here are milliseconds.
The examples given on the Mp3play home page are:
Copy Code code as follows:
Import Mp3play
filename = R ' C:\music.mp3 ' #播放c盘下的music. mp3 file
Clip = mp3play.load (filename)
Clip.play ()
Import time
Time.sleep (Min, Clip.seconds ()) #如果mp3文件的长度小于30少时, all played out, otherwise play only 30 seconds.
Clip.stop ()