Android Game Development: Building a game framework (2)

Source: Internet
Author: User

4. Audio module (audio)

Audio module programming has always been a complex topic. Here, we do not plan to use some advanced and complex audio processing methods, mainly playing background music. In writingCodeBefore, let's take a look at the basic knowledge of audio.

Sampling Rate: defines the number of samples that extract from continuous signals every second and form discrete signals. The higher the sampling rate, the better the sound quality. The unit is represented by Hz (HZ). CD is generally 44.1 kHz. For each sampling system, a certain storage bit (bit number) is allocated to express the acoustic amplitude state of the sound wave, which is called the sampling resolution or sampling accuracy. Each increase of 1 bit, the number of States expressing the acoustic amplitude is doubled, and the dynamic range State of 6 dB is increased. A 2-bit digital audio system expresses thousands of States, that is, the dynamic range of 12 dB, and so on. For example, 16 bits can express 65536 states, and 24 bits can express up to 16777216 states. The dynamic range refers to the range from the weakest to the strongest. The Auditory range of human ears is usually 20Hz ~ 20 kHz. High sampling rate means more storage space. For example, 60 s of sound, 8 KHz sampling rate, 8 bits, about 0.5 m, 44 KHz sampling rate, 16 bits, more than 5 m, ordinary 3-minute pop songs, it will exceed 15 MB.

Many good compression methods have been proposed to avoid quality reduction. For example, MP3s and oggs are popular compression formats in the network.

It can be seen that 3 minutes of songs occupy a lot of space. When playing back-end music of the game, we can stream the audio instead of pre-loading it To the memory. Generally, there is only one background music, so you only need to attach it to the disk once.

For some short sound effects, such as explosions and gun shoes, the situation is different. These short sound effects are often called multiple times at the same time. Streaming these sound effects from disks to each instance is not a good method. Fortunately, short sound effects do not occupy much memory space. Therefore, you only need to read these sound effects into the memory in advance, and then you can directly play these sound effects at the same time.

Therefore, our code must provide the following functions:

We need a method to load audio files for streaming playback (music) and in-memory playback (sound). It also provides the function of controlling playback.

There are three corresponding interfaces: audio, music, and sound. The Code is as follows.

Audio interface audio. Java

 
PackageCom. badlogic. androidgames. Framework;

Public InterfaceAudio {
PublicMusic newmusic (string filename );
PublicSound newsound (string filename );
}

The audio interface creates music and sound instances. A music instance represents a stream audio file, and a sound instance represents a short sound effect stored in the memory. Methods audio. newmusic () and audio. newsound () both use the file name as the parameter and throw an ioexception to prevent file loading failure (for example, if the file does not exist or is damaged ).

Music interface music. JVA

 Package Com. badlogic. androidgames. Framework;

Public Interface Music {

Public Void Play ();

Public Void Stop ();

Public Void Pause ();

Public Void Setlooping ( Boolean Looping );

Public Void Setvolume (Float Volume );

Public Boolean Isplaying ();

Public Boolean Isstopped ();

Public Boolean Islooping ();

Public Void Dispose ();
}

The music interface is a bit complex and contains methods for playing music streams, tentative and stopped, loop playback, and volume control (floating point numbers from 0 to 1. Of course, there are some getter methods to get the status of the current music instance. When we no longer need a music instance, we can destroy it (the dispose method), which will disable system resources, that is, streaming audio files.

 Sound InterfaceSound. Java

 
PackageCom. badlogic. androidgames. Framework;

Public InterfaceSound {

Public VoidPlay (FloatVolume );

Public VoidDispose ();

}

The sound interface is relatively simple and only contains the play () and dispose () methods. The former uses the specified volume as the input parameter, and we can play the sound whenever we need it. When we do not allow a sound instance, we need to destroy it to release the memory space it occupies.

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.