MediaPlayer, SoundPool, and AudioManager for Android Game Development

Source: Internet
Author: User

I. MediaPlayer
1. Obtain the MediaPlayer instance
[Java]
MediaPlayer. create (Context context, int resId); // parameter 1: Context object, parameter 2: Music Resource ID, which is generally placed in the raw folder. If there is no raw, create one by yourself.

2. Common MediaPlayer Functions
[Java]
Prepare (); // prepare for playing a music file
 
Start (); // play music
 
Pause (); // pause music
 
Stop (); // stop playing music
Note: The main difference between pause () and stop () is that you can call start () to resume playing after pause. After stopping a music, you need to call prepare () call start () to play the music.
 
3. Other common MediaPlayer Functions
[Java]
SetLooping (boolean looping); // sets whether the music is played cyclically. true indicates that the music is played cyclically.
 
SeekTo (int msec); // jump the music to a certain time point, in milliseconds
 
GetDuration (); // obtain the total time length of the music file to be played.
 
GetCurrentPosition (); // gets the time point of the current music.
 
Ii. AudioManager (music management class, which can obtain the current music size and maximum volume)
1. Obtain the AudioManager instance
[Java]
AudioManager am = new AudioManager ();
2. Common AudioManager Functions
[Java]
SetStreamVolume (IBD streamType, int index, int flags); // sets the volume. Parameter 1: music type (AudioManager. STREAM_MUSIC), parameter 2: volume size, parameter 3: set one or more flags
 
GetStreamVolume (int streamType); // gets the current volume. The parameter gets the volume type.
 
GetStreamMaxVolume (int streamType); // gets the maximum current volume. The parameter gets the type of the volume.
Note: function: Activity. setVolumeControlStream (int streamType) is used to control the volume. Parameter: volume type (AudioManager. STREAM_MUSIC)
Iii. SoundPool
1. Obtain the SoundPool instance
[Java]
New SoundPool (int maxStreams, int streamType, int srcQuality); // parameter 1: maximum sound value that can be played simultaneously; parameter 2: Sound type; parameter 3: Sound Quality
2. Common SoundPool Functions
[Java]
Int load (Context context, int resId, int priority); // loads the music file and returns the music ID (music stream file data) parameter 1: Context instance, parameter 2: music File ID. Parameter 3: indicates the sound with priority. Currently, it has no effect, but is of compatibility value. Generally, you can write a value of 1.
 
Int play (int soundId, float leftVolume, float rightVolume, int proority, int loop, float rate); // when playing music, 0 is returned for playback failure. If it is normal, a non-0 value is returned, parameter 1: The uploaded music file. Parameter 2: The left audio channel range of the volume is 0.0 ~ 1.0 parameter 3: Right channel of the volume, range: 0.0 ~ 1.0 parameter 4: Music stream priority. 0 indicates the lowest priority. Parameter 5: music playback times.-1 indicates an infinite loop. 0 indicates a normal loop. Otherwise, the loop times are displayed, parameter 6: playback speed. value range: 0.5 ~ 2.0, 1.0 indicates normal playback.
 
Pause (int streamID); // pause music playback. Parameter: Stream ID after music file loading
 
Stop (int streamID); // The Stream ID after the music file is loaded.
 
Release (); // release SoundPool Resources
 
SetLoop (int streamID, int loop); // sets the number of cycles. Parameter 1: ID of the stream loaded by the music file; parameter 2: number of cycles
 
SetReat (int streamID); // sets the playback rate.
 
SetVolume (int streamID, float leftVolume, float rightVolume); // set the volume. Parameter 2: Left-channel volume. Parameter 3: Right-channel volume.
 
SetPriority (int streamID, int priority); // sets the stream priority. Parameter 2: priority value.
 
Iv. MediaPlayer and SoundPool Analysis
1. Advantages and Disadvantages of MediaPlayer:
Disadvantages: high resource usage, long latency, and simultaneous playback of multiple audios are not supported. 1 ~ Latency of about 3 seconds; of course, this problem can be solved using MediaPlayer. seekTo.
Advantage: it supports playing a music file that hates large files, and does not need to be loaded and prepared for a period of time like SoundPool. MediaPlayer can play music in time.
2. advantages and disadvantages of using SoundPool:
Disadvantage: a maximum of 1 MB of memory space can be applied, meaning that users can only use some very short sound clips, rather than playing songs or game background music. SoundPool provides Pause and stop methods, but it is recommended that you do not use these methods easily, because using them will cause the program to be inexplicably terminated. We recommend that you use OGG to play audio in SoundPool. If you use an audio file in WAV format, an exception may occur during playback. When playing a music file using SoundPool, if you call the playing function in the construction to play the music, the effect is no sound! Not because the function is not executed, but because the SoundPool needs to load the preparation time! Of course, this preparation time is very short and will not affect the use, but the program has no sound at the beginning.
Advantages:
Supports simultaneous playback of multiple music files.
 
Through the above analysis, in Android game development, it is certainly better to use MediaPlayer for game background music than to use SoundPool, while playing game sound effects using SoundPool is better.
 

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.