Android 3d Game Research (1) (learn and write, thank you for your correction, bow): Sound

Source: Internet
Author: User

I wanted to learn 3d a long time ago, but I never had time. Recently, I was able to learn 3d;
Write the voice today:
There are two types of sounds in the game: Background Music and action music;
What is the difference between them: background music, which takes a long time relative to motion music. So in this way, I can cite it in two ways: normal music (background music ), pool playback (action music)

(1) pool playback:
Code implementation:

SoundPool sp; // gets a sound pool reference
HashMap <Integer, Integer> spMap; // gets a map reference.

// Initialization
Sp = new SoundPool (
5, // maxStreams parameter, which is used to set the number of sound effects that can be played at the same time
AudioManager. STREAM_MUSIC, // streamType parameter. This parameter sets the audio type, which is usually set to STREAM_MUSIC in games.
0 // srcQuality parameter, which sets the quality of the audio file. Currently, no effect is available. Set it to 0 as the default value.
);
SpMap = new HashMap <Integer, Integer> ();
SpMap. put (1, sp. load (this, R. raw. attack02, 1 ));
SpMap. put (2, sp. load (this, R. raw. attack14, 1 ));


// Play music
Public void playSound (int sound, int number) {// playback sound. The parameter sound is the id of the playback sound, and the parameter number is the number of playback sound effects.
AudioManager am = (AudioManager) this. getSystemService (this. AUDIO_SERVICE); // instantiate the AudioManager object
Float audioMaxVolumn = am. getStreamMaxVolume (AudioManager. STREAM_MUSIC); // returns the maximum volume of the current AudioManager object.
Float audioCurrentVolumn = am. getStreamVolume (AudioManager. STREAM_MUSIC); // return the volume of the current AudioManager object.
Float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
Sp. play (
SpMap. get (sound), // the id of the music to be played
VolumnRatio, // left audio volume
VolumnRatio, // right-channel volume
1, // priority, 0 is the lowest
Number, // number of cycles. 0 indicates no loops, and-1 indicates no permanent loops.
1 // playback speed. The value ranges from 0.5 to 2.0. 1 indicates the normal speed.
);
}


// Event reference:
PlaySound (); // play the first sound effect, loop through it
Sp. pause (spMap. get (1); // pause the first sound effect.
Sp. stop (spMap. get (1); // stop the first sound effect.

Well, the above is a simple use of pool playback;

(2) normal playback
MediaPlayer mp; // MediaPlayer reference
AudioManager am; // AudioManager reference

// Initialize the audio
Mp = new MediaPlayer ();

// Required before the first playback
Try {
Mp. setDataSource ("/sdcard/dl. mid"); // load the audio and enter the Initialized status.
} Catch (Exception e) {e. printStackTrace ();}
Try {
Mp. prepare (); // enter the prepared status.
} Catch (Exception e) {e. printStackTrace ();}

// Event reference: www.2cto.com
Mp. start (); // play music
Mp. pause (); // pause music
Mp. stop (); // stop music

// Mp. isPlaying (); // determines whether the video is in the playing status.

// Initialize the volume
Am = (AudioManager) this. getSystemService (this. AUDIO_SERVICE );
Am. adjustVolume (AudioManager. ADJUST_RAISE, 0); // increase the volume
Am. adjustVolume (AudioManager. ADJUST_LOWER, 0); // reduce the volume


Okay. The above is the simple voice scheduling;

 

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.