Reprint Please note: @ small Wuyi http://www.cnblogs.com/xiaowuyi
Pygame. mixer is a module used to process sound and its meaning is "mixer ". In the game, the processing of sound generally includes two parts: making sound and playing sound. Here, only the playing sound is learned.
1. Start and initialize pygame. Mixer
Pygame. mixer. INIT (): Initialize mixer.Program, Usually put in the first fewCodeMedium:
Import pygame
Pygame. INIT ()
Pygame. mixer. INIT ()
2. Play the audio clip WAV file
When playing the audio clip WAV file, pygame. mixer uses the sound object in the following format:
Soundwav = pygame. mixer. Sound ("filename.wav") Audio filename.wav file name
Soundwav. Play ()
In games, the following code is often used:
# @ Xiaowuyi http://www.cnblogs.com/xiaowuyi Import Pygame, syspygame. INIT () pygame. mixer. INIT () Screen = Pygame. display. set_mode ([640,480 ]) Pygame. Time. Delay ( 1000)# Wait 1 second for mixer to complete Initialization 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, and Ogg music files.
Here we only learned an MP3 file. If it is not a Windows system, try Ogg files. Pygame is used to play music files. the music module in mixer mainly uses the following methods:
pygame. mixer. music. load ("filenamegame")
pygame. mixer. music. play (n, start, stop) # The first parameter indicates the number of playbacks. If it is-1, it indicates loop playback. If it is omitted, it indicates playing only once. The second and third parameters indicate the start position and end position of the playback, respectively.
complete code:
# @ Small Five-definition http://www.cnblogs.com/xiaowuyi Import pygame, syspygame. 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 it starts playing music, it will continue to do the next thing, that is, if we want to play two songs, then
Pygame. mixer. Music. Load ("filename1.mp3 ")
Pygame. mixer. Music. Play ()
Pygame. mixer. Music. Load ("filename2.mp3 ")
Pygame. mixer. Music. Play ()
After this code is run, two songs are playing at the same time. In reality, we often need to finish playing one and then play another, so we need to use pygame. mixer. music. the get_busy () function determines that if the return value of the function is true, it indicates that the function is currently busy. Otherwise, false is returned. For example:
# @ Xiaowuyi http://www.cnblogs.com/xiaowuyi Import Pygame, syspygame. 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) # Wait 5 seconds until the filename.wav playback ends. SYS. Exit ()
4. Volume Control
pygame. mixer. Music. set_volume () is used to control the volume. The value ranges from 0. 0 is the minimum value, and 1 is the maximum value.
5. Problems Encountered during MP3 player preparation
Python + wxpython + pygame can easily create an MP3 player, but there are two problems encountered during preparation:
1. display of Chinese Song names, which has been solved by searching, plus # encoding = 'gbk'
2. When pygame plays an MP3 file, it is found that not all MP3 files are played, and the playing sound is often incorrect. Sometimes it is only a noise. After communication with the python learning and communication group (2), the author suggested that MP3 files with a speed of kbps can be played, but after several kbps MP3 tests on the Internet, abnormal playback still exists. I don't know which Daniel can solve this problem. I hope I can tell you the solution. Thank you.
therefore, if you plan to write an MP3 playing tool, python can provide many MP3 playing modules. Here, I think it is easier to use the mp3play module. The current version is mp3play 0.1.15, Which is http://pypi.python.org/pypi/mp3play /. Here we will briefly introduce the main method:
play (n, starms, endms): play, the first parameter indicates the number of times, and the second and third parameters indicate the start and end positions of the play, the Unit is milliseconds.
stop (): Stop.
pause (), unpause (): pause and start.
isplaying (): determines whether the video is being played. If true is returned, the video is being played.
ispaused (): checks whether it is paused. If true is returned, it indicates paused.
seconds (): the total number of seconds for returning MP3 files. Note that the unit here is seconds.
milliseconds (): the total number of milliseconds in which the MP3 file is returned. Note that the unit here is milliseconds.
the example on the mp3play homepage is as follows:
#@ Xiaowuyi http://www.cnblogs.com/xiaowuyiImportMp3playfilename= R'C: \ musicloud'#The musicloud file in the C driveClip =Mp3play. Load (filename) clip. Play ()ImportTimetime. Sleep (min (30, Clip. Seconds ()))#If the length of an MP3 file is less than 30 seconds, the video is played completely. Otherwise, the video is played for only 30 seconds.Clip. Stop ()