SDL study note 5 (music playback)

Source: Internet
Author: User

I always wanted to write something like a player, but I lacked the knowledge of decoding audio files, so I was too lazy to learn it. After all, I was eager to write a decent software, and it took too much time to write a decoder by myself, in addition, it cannot be ensured that most of the cases can be handled, and there will inevitably be bugs. Fortunately, SDL once again provides the extended library sdl_mixer, which itself has too few supported formats, but the extended library already supports most of the formats, now let's take a look at these simple and common functions!

Int mix openaudio (INT frequency, uint16 format, int channels, int chunksize)

This function enables audio in a certain way. The first parameter is the frequency Hz/S. Generally, the mix Default Frequency (22050) is used)

The second parameter is the format (I don't know what it is =). Use the default mix default format.

The third parameter indicates the audio channel. 1 indicates the single channel, 2 indicates the dual channel, that is, the stereo sound.

The last parameter is probably a buffer or something. It is unclear. Generally, it is set to 4086. If it is too small, it seems to occupy too much CPU and is too big, it is not good.


The default parameter is used to enable the audio. Then we can start loading and playing the music.


Mix_music * Mix loadmus (const char * file)

This function loads the music file specified by file, as long as it is in a supported format.

The returned structure is used for playback.

Int mix playmusic (mix_music * Music, int loops)

Play the specified music and play the loops more than once in a loop. If the loops value is-1, the loop is infinite.

Note that the playmusic function is returned from the playmusic function. Therefore, the playmusic function exits after the music is stopped.

Function used to determine whether the music is being played

Int mix playingmusic ()


If a music file is no longer played, you need to release the space.

Void mix freemusic (mix_music * music)


Of course, if it is too monotonous to play a music, and the game often needs the sound of action, let's take a look at how to produce sound effects.

Mix_chunk * Mix loadwav (char * file)

Similarly, we use this function to load the audio effect file. The returned structure is used to play the audio effect.


Int mix playchannel (INT channel, mix_chunk * chunk, int loops)

This function is used to play the sound effect.

The first parameter specifies the audio track for playing. Generally, the value-1 indicates the first idle audio track.

The second parameter is the sound effect file.

The third parameter is the number of cycles, which is the same as the number of music cycles.


Of course, the corresponding functions are needed to sweep the end.

Void mix freechunk (mix_chunk * chunk)


Turn off the audio after playing the video.

Void mix closeaudio ()

The above functions should be sufficient for basic applications and can be used with a maximum of a few more control functions. However, in general, the game sounds out of the box and the size is not controlled. Haha

So I am writing this article for the time being. after learning the basic part of SDL, I will summarize it in a player.

well, you can go to the official documents for the remaining items, by the way, we recommend some website http://bbs.9ria.com/thread-65247-1-1.html of sound effect files

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.