public void initSound(){ bgMusic[0]=MediaPlayer.create(this, R.raw.screen_bg); bgMusic[0].setLooping(true); bgMusic[0].setVolume(0.3f, 0.3f); soundPool=new SoundPool(4, AudioManager.STREAM_MUSIC, 100); soundMap=new HashMap<Integer, Integer>(); soundMap.put(<span style="color:#ff0000;">0</span>, soundPool.load(this, R.raw.awp_fire,1)); soundMap.put(<span style="color:#ff0000;">1</span>, soundPool.load(this, R.raw.awp_fire,1)); } public void playSound(int <span style="color:#ff0000;">sound</span>,int<span style="color:#3333ff;"> loop</span>){ if(!musicFlag){ return; } AudioManager manager=(AudioManager) this.getSystemService(Context.AUDIO_SERVICE); float streamVolumeCurrent=manager.getStreamVolume(AudioManager.STREAM_MUSIC); float streamVolumeMax=manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume=streamVolumeCurrent/streamVolumeMax; soundPool.play(soundMap.get(<span style="color:#ff0000;">sound</span>), volume, volume, 1, <span style="color:#3333ff;">loop</span>, 0.5f); }
0和1表示可选择的声音资源!
act_Main.playSound(<span style="color:#ff0000;">1</span>, 0);