Chapter 2 music and sound effects
The advertisement says "there is no sound, no sound, no matter how good it is". Let's add the sound effect to the program.
In general, the game sounds are divided into two parts: music and sound. The intuitive difference is that the playing time of music is long and the resource file is large. The playback time of sound effects is short, and the resource files are small. In Android, we use two different methods to play music and sound effects.
The mediaplayer class is used to play music. mediaplayer is complicated. Here we will only give a brief introduction to it to meet the basic requirements of playing music. For more information, see the content in the help document.
For ease of calling, we create an util class to encapsulate the method for playing background music into util.
Public ClassUtil {
StaticMediaplayerMP;
Static VoidPlaybgm (string path,BooleanLooping ){
If(MP=Null){
MP=NewMediaplayer ();
}
MP. Reset ();
Try{
MP. Setdatasource (PATH );
MP. Prepare ();
}Catch(Illegalargumentexception e ){
//TodoAuto-generated Catch Block
E. printstacktrace ();
}Catch(Illegalstateexception e ){
//TodoAuto-generated Catch Block
E. printstacktrace ();
}Catch(Ioexception e ){
//TodoAuto-generated Catch Block
E. printstacktrace ();
}
MP. Setlooping (looping );
MP. Start ();
}
}
Let's analyze this code: first, we need a mediaplayer instance, and then reset the mediaplayer to the idle status. Note: This operation is required. If you do not reset the status and call setdatasource directly, an exception is thrown. Specify the file to be played and call the prepare method for preparation. If you want to specify the start time (seekto), it must be performed after prepare. You can also set whether to loop, and finally call start to play the video.
After encapsulation, we can easily use this function. We need to reload the start method in scenestartmenu.
Public VoidStart (surfaceholder ){
Util.Playbgm("/Sdcard/sample. Mid ",False);
}
In this way, the program starts playing music. Note that the resources occupied by mediaplayer should be released as appropriate.
Run the edited code to check the effect. Sample. Mid is in the Res/raw directory.
Next let's learn how to play a sound effect. It will use a different soundpool method. Although soundpool is based on mediaplayer, It is optimized to play multiple files at the same time and is not suitable for playing large files. It can be said that it is customized for playing sound effects. Let's take a look at the playback sound code below:
Private Static Int Max_channel= 6;
Private StaticSoundpoolSoundpool;
Private StaticHashmap Soundpoolmap;
Private StaticAudiomanagerAudiomanager;
First, set the number of audio channels. If the number of audio streams played at the same time exceeds this value, the first audio will be disabled. Then define the soundpool variable. Pay attention to the soundpoolmap variable, which will be explained below.
Public Static VoidPlayse (string path,FloatVolume,IntLoop,FloatRate ){
If(Soundpool=Null){
Soundpool=NewSoundpool (Max_channel, Audiomanager.Stream_music, 10 );
}
If(Soundpoolmap=Null){
Soundpoolmap=NewHashmap ();
}
If(!Soundpoolmap. Containskey (PATH )){
Soundpoolmap. Put (path,Soundpool. Load (path, 1 ));
}
Here we will explain soundpoolmap. You need to load the audio into the memory before it is played. After it is loaded, you will get a streamid. Many functions in soundpool use this ID to control specific audio streams. Therefore, we save this ID to map and correspond to the path of the audio file. You can find the audio stream in the path.
If(Volume
If(Audiomanager=Null)
Audiomanager= (Audiomanager) Main.Getinstance()
. Getsystemservice (context.Audio_service);
Volume =Audiomanager. Getstreamvolume (audiomanager.Stream_music);
}
While(Soundpool. Play (Soundpoolmap. Get (PATH ),
Volume, volume, 0, loop, rate) = 0 );
}
This method is not very scientific. It is best to load audio during initialization. You can write another load method.
Public VoidPlayse (string path ){
Playse(Path,-1f, 0, 1f );
}
Public VoidStopse (string path ){
If(Soundpool! =Null&&Soundpoolmap! =Null
&&Soundpoolmap. Containskey (PATH )){
Soundpool. Stop (Soundpoolmap. Get (PATH ));
}
}
Modify the start function in scenestartmenu to test the function.
Public VoidStart (surfaceholder ){
// Util. playbgm ("/Sdcard/Sample. Mid ", false );
Util.Playse("/Sdcard/system11.ogg ");
Util.Playse("/Sdcard/system12.ogg ");
}
Now, this tutorial can finally come to an end. Although it is a bit tricky, I have probably explained the knowledge used to create a simple game. If there are still deficiencies, please leave a message to add, and everyone will learn from each other and improve together. In the end, I did not complete the tank war in the tutorial, but I will design a new game based on the tank war, instead of using Java, but a brand new cross-platform framework. I will share my experiences with you later.
This chapter sample program http://u.115.com/file/f1435ee7e8