Android game development

Source: Internet
Author: User

In today's article, we will show you how to create, manage, and useAndroid. In the first section, we will demonstrate the basic methods for encapsulating Code related to sound management. This method works normally when you have a typical application or a single-threaded simple game. After reading this, you can read the second part, which will tell you a more advanced way to manage sounds through other classes.

The method we use to play a soundSound poolClass insteadAndroidRecommended by the development wizardMedia PlayerClass. Of course, useMedia PlayerClass implementation is also possible for simple applications, but this class cannot provide the required flexibility.

The following is the code. First, we need to createSound ManagerClass to encapsulate all sound-related code.

Java code:
Publicclasssoundmanager {
  
Privatesoundpoolmsoundpool;
Privatehashmapmsoundpoolmap;
Privateaudiomanagermaudiomanager;
Privatecontextmcontext;
}


The first part of the code is to create the required member variables:

Msoundpool: we use this object provided by Android to create and play sound;
Msoundpoolmap: Once the sound is loaded, it is stored in a hash;
Maudiomanager: handle for playing the services we want music.
Mcontext: the handle of a program context.

Java code:
Publicvoidinitsounds (contextthecontext ){
Mcontext = thecontext;
Msoundpool = newsoundpool (4, audiomanager. stream_music, 0 );
Msoundpoolmap = newhashmap ();
Maudiomanager = (audiomanager) mcontext. getsystemservice (context. audio_service );
}

The first function is createdSound Manager. At the beginning, we pass the program context to it and get a handle to it. Create next lineSound poolObject. The first parameter indicates the number of audio streams that we want to have at the same time. In other words, how many sounds can be played simultaneously. If you try to play audio that is more than this integer value, it will automatically terminate the earliest audio stream.

The following two lines create a hash and hand it overAudio-Manager.

Java code:
Publicvoidaddsound (intindex, intsoundid)
{
Msoundpoolmap. Put (index, msoundpool. Load (mcontext, soundid, 1 ));
}

We add audio in this function. Each audio file gets an index for playback. Make sure that each audio file has a different index.SoundidThe parameter is from the original audio file. You can pass this parameter by callingR. Raw. mysoundTo get the nameMysoundAudioID. The following describes how to use this function.

Java code:
Publicvoidplaysound (intindex)
{
Floatstreamvolume = maudiomanager. getstreamvolume (audiomanager. stream_music );
Streamvolume = streamvolume/maudiomanager. getstreammaxvolume (audiomanager. stream_music );
Msoundpool. Play (msoundpoolmap. Get (INDEX), streamvolume, streamvolume, 1, 0, 1f );
}
  
Publicvoidplayloopedsound (intindex)
{
Floatstreamvolume = maudiomanager. getstreamvolume (audiomanager. stream_music );
Streamvolume = streamvolume/maudiomanager. getstreammaxvolume (audiomanager. stream_music );
Msoundpool. Play (msoundpoolmap. Get (INDEX), streamvolume, streamvolume, 1,-1, 1f );
}

These two functions control sound playback. You only need to passAddsound ()The index number of the created audio.StreamvolumeVariable is used to obtain the volume of the media file set on the current device. Because we do not want the audio playback volume to be different from the volume set on the mobile phone.

You need to divide the obtained volume by the maximum streaming media volume to obtainFloating Point number between 0 and 1. This is required for playback. Thank you for choosingGyuriPoint this problem to me.

Msoundpool. PlayThe function is quite simple. First, you must pass the index number of the audio you want to play. The next two parameters are the lower limit and upper limit of the volume during playback. I strongly recommend that you set both values to the audio volume.

The next variable is the audio priority. This is not yet implemented, so it is better to set it to 1. The next parameter is to set whether the audio is played cyclically. -1 is set to loop, and 1 is set to single playback. This is the only difference between the two functions. I personally prefer devices that allow me to set loop playback.

The last parameter affects the playback sampling rate. 1 is the normal rate, and the range can be set from 0.5 to 2.0 corresponding to the half-speed and double-speed playback.

It is very simple to use this class. What you need to do is to create a member variable when you need audio. Then you can initializeSound ManagerAnd add the audio in the following way.

Java code
 
Msoundmanager = newsoundmanager ();
Msoundmanager. initsounds (getbasecontext ());
Msoundmanager. addsound (1, R. Raw. Sound );

Then play the audio Based on the index.


Java code:
  
Msoundmanager. playsound (1 );

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.