The Pygame.mixer is a module for handling sound, meaning "mixer". The processing of sound in the game typically involves making sounds and playing sounds, where only the sound portion of the play is learned.
1. Pygame.mixer Startup and initialization
Pygame.mixer.init (): Initialization of the mixer. program, when used, is generally placed in the first few lines of code:
The code is as follows:
Import Pygame
Pygame.init ()
Pygame.mixer.init ()
2. Play sound clip wav file
When you play a sound clip wav file, Pygame.mixer uses the audio object in the format:
The code is as follows:
Soundwav=pygame.mixer.sound ("Filename.wav") #filename. wav file name
Soundwav.play ()
In the game, the following code is often used to complete:
The code is as follows:
Import Pygame,sys
Pygame.init ()
Pygame.mixer.init ()
Screen=pygame.display.set_mode ([640,480])
Pygame.time.delay (#等待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
Here just learned a MP3 file, if non-Windows system, that can try ogg file. Playing music files mainly uses the music module in Pygame.mixer, the Main method is:
The code is as follows:
Pygame.mixer.music.load ("Filename.mp3")
Pygame.mixer.music.play (n,start,stop) #第一个参数为播放次数, if 1 means loop playback, ellipsis means only 1 plays. The second parameter and the third parameter represent the starting and ending positions of the playback, respectively.
The complete code is:
The code is 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, it will continue to do the next thing once it starts playing music, that is, if we want to play two songs, then
The code is as follows:
Pygame.mixer.music.load ("Filename1.mp3")
Pygame.mixer.music.play ()
Pygame.mixer.music.load ("Filename2.mp3")
Pygame.mixer.music.play ()
Once this code is run, there will be two songs playing at the same time. In practice, we often need to play a song, and then play another song, 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:
The code is 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 (#等待5秒让filename). WAV Playback End
Sys.exit ()
4. Control the 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 player encountered problems
Python+wxpython+pygame can easily and conveniently make a MP3 player, but there are two issues encountered in the production:
One is the display of Chinese song name, through the search has been resolved, plus # encoding= ' GBK '
Second, when playing MP3 files, Pygame found that not all of the MP3 files have played, often play sound is not correct, and sometimes just miscellaneous noise. After すeasyげ communication with the Python Learning Communication Group (2), すeasyげ proposed that the mp3 file of bit-speed 128kbps can be played, but after several 128kbps MP3 tests from the Internet, it is found that the playback is still abnormal. I do not know which Daniel can solve this problem, hope to be able to tell the solution, thank you.
Therefore, if you are going to write a mp3 play tool, Python can do mp3 play a lot of modules here, I think you can consider the use of Mp3play module, more convenient and simple. The current version is Mp3play 0.1.15, the specific download address is http://pypi.python.org/pypi/mp3play/. Here is a brief introduction to its main methods:
Play (N,STARMS,ENDMS): Play, the first parameter represents the number of times, and the second to third parameter represents the starting and ending position of the playback, in milliseconds.
Stop (): Stop.
Pause (), unpause (): Pause and start.
IsPlaying (): Determines if the song is playing, if it returns true.
Ispaused (): Determines whether a pause is paused, or true if it is returned.
Seconds (): Returns the MP3 file for a number of seconds, note that the units here are seconds.
Milliseconds (): Returns the MP3 file for a total of milliseconds, note that the units here are milliseconds.
The examples given on the Mp3play homepage are:
The code is 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少时, play all, or only 30 seconds.
Clip.stop ()