Android game development-01-sound soundpool

Source: Internet
Author: User
Tags android games

According to different sounds, music is usually divided into two parts: music and sound effects. Music can be used for background music and is in consideration of performance. If long music is stored in the memory, resource shortage may occur, android is required for general sound effects. media. soundpool implementation. The procedure is as follows:

1. constructor soundpool (INT maxstreams, int streamtype, int srcquality)

  • Maxstream this parameter is used to set the number of sound effects that can be played at the same time. If it is set to 4, four sound effects can be played at the same time.
  • Streamtype this parameter is used to set the audio type. The default value is stream_music.
  • Srcquality this parameter is used to set the audio quality. 0 is the default value.

2. Load the audio file public int load (context, int resid, int priority)

Resid is the resource ID.

Priority is the priority

3. Play public final int play (INT soundid, float leftvolume, float rightvolume, int priority, int loop, float rate)

Soundid: ID of the music to be played

Leftvolume: volume of the left audio channel

Rightvolume right-channel volume

Priority priority

Number of loop cycles. 0 indicates no loop, and-1 indicates an infinite loop.

Rate playback speed. The value ranges from 0.5 to 2.0. 1 indicates the normal speed.

The following is an example. The layout will not be pasted. There are four buttons.

Package COM. example. demogame; import Java. util. hashmap; import android. app. activity; import android. media. audiomanager; import android. media. soundpool; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class mainactivity extends activity {private button button1; private button button2; private button button3; private button button4; private soundpool sp; private hashmap <integer, integer> spmap; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); super. setcontentview (R. layout. activity_main); this. initsoundpool (); button1.setonclicklistener (New onclicklistener () {public void onclick (view v) {playsound (1, 1) ;}}); button2.setonclicklistener (New onclicklistener () {public void onclick (view v) {sp. pause (spmap. get (1) ;}}); button3.setonclicklistener (New onclicklistener () {public void onclick (view v) {playsound (2, 1 );}}); button4.setonclicklistener (New onclicklistener () {public void onclick (view v) {sp. pause (spmap. get (2) ;}}) ;}private void initsoundpool () {button1 = (button) findviewbyid (R. id. button1); button2 = (button) findviewbyid (R. id. button2); button3 = (button) findviewbyid (R. id. button3); button4 = (button) findviewbyid (R. id. button4); SP = new soundpool (5, audiomanager. stream_music, 0); spmap = new hashmap <integer, integer> (); spmap. put (1, SP. load (this, R. raw. qiang00, 1); spmap. put (1, SP. load (this, R. raw. qiang01, 2);}/*** @ Param sound * Music ID * @ Param Number * loop times */private void playsound (INT sound, int number) {audiomanager AM = (audiomanager) This. getsystemservice (this. audio_service); float audiomaxvolumn = aM. getstreammaxvolume (audiomanager. stream_music); float audiocurrentvolumn = aM. getstreamvolume (audiomanager. stream_music); float volumnratio = audiocurrentvolumn/audiomaxvolumn; SP. play (spmap. get (sound), volumnratio, volumnratio, 1, number, 1) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

Preface: for readers:

Starting from today, I am going to carefully study android game development. If any article helps you, I hope to leave a message to let me know that I still have like-minded people. Of course, I hope you will not be able to give me some advice on where to write well. Since we have recently started to develop Android games, if you feel that the quality is poor, please forgive me for it. I will definitely continue to write it. If there is another reprint, you don't need to specify the source. I think it is my pleasure to help everyone. Heheh hopes to become friends with android game experts.

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.