Python plays MP3 with the Pygame module

Source: Internet
Author: User

Install Pygame (This is the python3,32 bit)

PIP installs this WHL file

Just run the code when you're done, it's short.
import timeimport pygamefile=r‘C:\Users\chan\Desktop\Adele - All I Ask.mp3‘pygame.mixer.init()print("播放音乐1")track = pygame.mixer.music.load(file)pygame.mixer.music.play()time.sleep(10)pygame.mixer.music.stop()
Function: Play music after 10 seconds stop appendix

Pygame.init () initializes all modules,
Pygame.mixer.init () or just initialize the audio section
Pygame.mixer.music.load (' Xx.mp3 ') uses filename as a parameter to load music, music can be in Ogg, MP3 and other formats. The loaded music does not all go into the content, but is played as a stream, that is, when it is played, it is read from the file a little bit.
Pygame.mixer.music.play () plays the loaded music. The function returns immediately and the music is played in the background.
The play method can also use two parameters
Pygame.mixer.music.play (loops=0, start=0.0) loops and start respectively represent the number of repetitions and where the playback begins.
Pygame.mixer.music.stop () stops playing,
Pygame.mixer.music.pause () pauses playback.
Pygame.mixer.music.unpause () cancels the pause.
Pygame.mixer.music.fadeout (time) is used to fade out, and the volume is gradient from the initial value to 0 in times of milliseconds and finally stops playing.
Pygame.mixer.music.set_volume (value) to set the volume of playback, the volume value can range from 0.0 to 1.0.
Pygame.mixer.music.get_busy () to determine if the music is playing, return 1 is playing.
Pygame.mixer.music.set_endevent (Pygame. Userevent + 1) When the music playback is complete, notify the user program by event, and set the Pygame to be sent when the music playback is complete. Userevent+1 event to the user program. Pygame.mixer.music.queue (filename) uses the specified next music file to play, and the current music playback automatically starts playing the specified next. Only one music file waiting to be played can be specified at a time.

Python plays MP3 with the Pygame module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.