One of Android audio playback, android audio playback

Source: Internet
Author: User

One of Android audio playback, android audio playback
SoundPool Introduction


In addition to playing audio using MediaPlayer, Android also provides SoundPool for playing sound effects. SoundPool uses the sound effect pool to manage multiple short-term sound effects. For example, it can load 20 sound effects at the beginning, in the future, play the video by the sound ID in the program.
I. Features of SoundPool


SoundPool is mainly used to play some shorter sound clips. Compared with MediaPlayer, SoundPool has the advantage of low CPU resource occupation and low response latency. In addition, SoundPool also allows you to set parameters such as sound quality, volume, and playback ratio.


SoundPool provides a constructor that specifies the total number of sounds (that is, the size of the pool) supported by the constructor and the quality of the sound. The constructor is as follows:


SoundPool (int maxStreams, int streamType, int srcQuality): the first parameter specifies the number of sounds supported. The second parameter specifies the sound type. The third parameter specifies the sound quality.


Ii. SoundPool Initialization
Once the SoundPool object is obtained, you can call multiple load methods of SoundPool to load the sound.


SoundPool provides the following four load methods:


Int load (Context context, int resld, int priority): loads sound from the resource corresponding to the resld.


Int load (FileDescriptor fd, long offset, long length, int priority): loads the sound of the start and length of the offset of the file corresponding to fd.


Int load (AssetFileDescriptor afd, int priority): loads sound from the file corresponding to afd.


Int load (String path, int priority): loads sound from the file corresponding to the path.


There is a priority parameter in the above four methods. This parameter does not work yet. Android recommends setting this parameter to 1 to maintain compatibility with the future.


After the above four methods load the sound, the ID of the sound will be returned, and then the program can play the specified sound through the sound ID.




Iii. Method for playing a specified sound in SoundPool:


Int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate): the first parameter of this method specifies which sound to play; leftVolume and rightVolume specify the volume on the left and right: priority specifies the playback sound priority. A greater value indicates a higher priority. loop indicates whether to loop. 0 indicates no loop, and-1 indicates a loop; rate specifies the playback rate. The value ranges from 0.5 to 2. 1 indicates the normal rate.


To better manage the 1D of each sound loaded by the SoundPool, the program generally uses a HashMap <Integer, Integer> object to manage the sound.


To sum up, follow these steps:


1. Call the SoundPool constructor to create the SoundPool object.


2. Call the load () method of the SoundPool object to load sound from a specified resource or file. It is best to use HashMap <Integer, Integer> to manage the loaded sound.


3. Call the play method of SoundPool to play the sound.


The following program demonstrates how to use SoundPool to play sound effects.


The program code is as follows:

// Declare the variable SoundPool mSoundPool; // play action sound id int mPlaySound; // stop action play sound id int mPauseSound; // instantiate SoundPool mSoundPool = new SoundPool (5, AudioManager. STREAM_SYSTEM, 5); // initialize the playing action audio mPlaySound = mSoundPool. load ("/system/media/audio/ui/SoundRecorderPlay.ogg", 1); // initialize the audio mPauseSound = mSoundPool. load ("/system/media/audio/ui/SoundRecorderPause.ogg", 1); // play the playing action audio mSoundPool. play (mPlaySound, 1.0f, 1.0f, 0, 0, 1); // stop playing audio mSoundPool. play (mPauseSound, 1.0f, 1.0f, 0, 0, 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.