pygame study notes (4) : voice control

  • 2020-05-10 18:22:44
  • OfStack

pygame.mixer is a module for processing sound. It means "mixer". The processing of sound in the game includes two parts: making sound and playing sound.

1. pygame.mixer startup and initialization

pygame.mixer.init (): initialization of mixer. In the program, when used, 1 is usually placed in the first few lines of code:


import pygame
pygame.init()
pygame.mixer.init()

2. Play the sound clip wav file
When playing the sound clip wav file, pygame.mixer USES the Sound object, and the format is:

soundwav=pygame.mixer.Sound("filename.wav") #filename.wav The file name
soundwav.play()

In games, this is usually done with the following code:


import pygame,sys
pygame.init()
pygame.mixer.init()
screen=pygame.display.set_mode([640,480])
pygame.time.delay(1000)# Waiting for the 1 Seconds to mixer 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 I just learned one mp3 file. If you are not using windows system, you can try ogg file. The music module in pygame. mixer is mainly used to play music files. The main methods are as follows:

pygame.mixer.music.load("filename.mp3")
pygame.mixer.music.play(n,start,stop)# The first 1 Is the number of times played, if yes -1 Represents looping, while omitting means playing only 1 Times. The first 2 The number of parameters and the number of parameters 3 Two parameters represent the start and end positions of the playback respectively.

The complete code is:


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 it starts playing music, it will move on to the next thing, which is to say, 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()

When this code is run, it will appear that two songs are playing at the same time. Practice, we often need to play out 1 first, then the other 1 first, so you need to use pygame. mixer. music. get_busy () function to judge, if the function return value for True so that currently is in a state of busy, otherwise returns False. Such as:


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)# Waiting for the 5 Seconds to filename.wav End of the play
        sys.exit()

4. Control the volume

pygame. mixer. music. set_volume () is used to control the volume and value range of 0-1.0 - the floating point number. 0 is the minimum and 1 is the maximum.

5. Problems encountered in the production of mp3 player

python+wxpython+pygame can easily and conveniently make one mp3 player, but there are two problems in the production:

1 is the display of Chinese song name, which has been solved through search, plus # encoding='GBK'.
When pygame played mp3 files, it was found that not all mp3 files were played. Often, the sound was not played correctly, and sometimes there was only noise. After communication with python study group (2) the す easy げ exchanges, す easy げ forward speed 128 kbps mp3 files can play, but from the Internet under several 128 kbps mp3 testing, abnormal situation to play is still there. I don't know which Daniel can solve this problem. I hope you can tell me the solution. Thank you.

Therefore, if you plan to write a playback tool of mp3, python can play mp3 in many modules. Here, I think we can consider using mp3play module, which is more convenient and simple. The current version for mp3play 0.1.15, specific download address for http: / / pypi python. org/pypi/mp3play /. Here is a brief introduction to 1 of its main methods:

play(n,starms,endms): play, the first parameter is the number of times, and the second parameter is the starting and ending position of play, in milliseconds.
stop () : stop.
pause(), unpause(): pause and start.
isplaying(): determines if it is playing. If it returns TRUE, it means the song is playing.
ispaused(): determines if it is paused, and if it returns TRUE, it is paused.
seconds(): how many seconds do you return to mp3? Note that the units are seconds.
milliseconds(): how many milliseconds does it take to return the mp3 file?
The example given on the homepage of mp3play is:


import mp3play
filename = r'C:\music.mp3'# play c Under the plate music.mp3 file
clip = mp3play.load(filename)
clip.play()
import time
time.sleep(min(30, clip.seconds()))# if mp3 The length of the file is less than 30 Short, all play, otherwise only play 30 Seconds.
clip.stop()


Related articles: