Back to project page NotATop.
The source code is released under:
GNU General Public License
If you think the Android project NotATop listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.gg.module; /*w w w.j a va2 s.c o m*/ import java.util.HashMap; import java.util.Map; import com.gg.view.R; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; public class SoundControl { private MediaPlayer music ; //music ???? public AudioManager am; //????????????????? private SoundPool soundPool ; // short music private boolean soundOn ; //short music control private Context context ; private Map<Integer , Integer> soundMap; //??????Id????????????Id????????? public SoundControl(Context context){ init(context); } //?????????????? private void initMusic(){ music = MediaPlayer.create(context, R.raw.backmusic); music.setLooping(true); } //?????????????? private void initSound(){ soundPool = new SoundPool(10,AudioManager.STREAM_MUSIC,100); soundMap = new HashMap<Integer,Integer>(); soundMap.put(R.raw.choose, soundPool.load(context, R.raw.choose,1)); soundMap.put(R.raw.end, soundPool.load(context, R.raw.end,1)); soundMap.put(R.raw.pai, soundPool.load(context, R.raw.pai,1)); soundMap.put(R.raw.runing, soundPool.load(context, R.raw.runing,1)); } //???????????????? public void init(Context c){ context = c ; initMusic(); initSound(); } //???????????? public boolean isSoundOn(){ return soundOn; } //??????????? public void playSound(int resId){ am=(AudioManager)context.getSystemService(context.AUDIO_SERVICE); float audioMaxVolumn=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //??????AudioManager????????????? float audioCurrentVolumn=am.getStreamVolume(AudioManager.STREAM_MUSIC);//??????AudioManager?????????? float volumnRatio=audioCurrentVolumn/audioMaxVolumn; if(isSoundOn() == false)//???????????? return ; soundPool.play( soundMap.get(resId), //?????????id volumnRatio, //?????????? volumnRatio, //?????????? 1, //???????0???? 0, //?????????0????????-1???????? 1 //?????? ???????0.5-2.0???1???????? ); Integer soundId = soundMap.get(resId); if(soundId != null) soundPool.play(soundId, 1, 1, 1, 0, 1); } //??????? public void setMusic(){ if(music.isPlaying()){ music.pause(); soundOn = false; } else{ music.start(); soundOn = true; } } //???????? public void paisound(){ playSound(R.raw.pai); } public void end(){ playSound(R.raw.end); } public void choose(){ playSound(R.raw.choose); } public void running(){ playSound(R.raw.runing); } //?????????????? public void releasemusic(){ music.release(); soundPool.release(); } }