Pygame Loading Audio

Source: Internet
Author: User

Python learning:pygame loading the WinSound module from the audio python

The Beep method in the WinSound module can invoke the system's buzzer, accepting a frequency parameter of frequency (ranging from 37 to 36727) and a duration length parameter. Frequency too must not difficult to hear.

def beep( freq, dura ):    freq = math.floor( freq )    winsound.Beep( freq, dura )

In addition, WinSound also provides playback support for WAV files, which can be used to winsound.PlaySound( sound, flags ) play audio in WAV format (WAV format only).

Pygame Module

Playing sounds can use the Pygame module, where the mixer Submodule provides a series of APIs for playing sounds, which is very simple to use.

Music Sub-module
import pygame, time, osmixer = pygame.mixermixer.init( 11025 )music = mixer.musicfilename = 'mountain.mp3'def playMountain():    music.load( filename )    music.set_volume( 10 )    music.play()    while( music.get_busy() ):        time.sleep( 1 )    music.stop()    playMountain()

Call Mixer's Init method to initialize, then get the Mixer music object, the Load method is used to load the audio file (can load a variety of different formats of audio files, the above example loaded in the MP3 format of the file), using the Music.play () method can start The audio is loaded.

However, it is important to note that although the music object is actually loading audio (Music.get_busy () returns True), if there is no looping statement in the code above, the music will not play when the Python file is executed. The reason is that the audio file is played as a stream and requires a time.sleep( 1 ) deferred script execution time.

The Sound class in mixer

The sound class is also available in pygame.mixer for loading sounds:

import pygame, time, osmixer = pygame.mixermixer.init( 11025 )music = mixer.musicpath = os.pathcurr_dir = path.dirname( path.abspath( __file__ ) )def playSound( file_path ):    sound = mixer.Sound( file_path )    sound.set_volume( 5 )    channel = sound.play()    # print( sound.get_length() )    # while channel.get_busy() :        # time.sleep( 1 )    sound.stop()try:    file_path = path.join( curr_dir, 'mountain.mp3')    playSound( file_path )finally:    try:        file_path = path.join( curr_dir, 'mountain.wav')        playSound( file_path )    finally:        file_path = path.join( curr_dir, 'secosmic_lo.wav')        playSound( file_path )

The use of Pygame.mixer.Sound is similar to that of music objects, but it is not necessary to set loops and play sounds normally.

Note that in the above code, Mountain.mp3 is a light music file downloaded from NetEase Cloud, mountain.wav is to change the MP3 suffix to the wav suffix directly, sscosmic_lo.wav is a sample audio provided in Pygame. Mountain.mp3 and Mountain.wav files have failed to play, only the WAV format secosmic_lo.wav can play properly. Visible sound can only play files in WAV format.

Reference
    1. Play Music: Pygame.mixer.music
    2. Stackoverflow:pygame, sounds don ' t play

Pygame Loading Audio

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.