private SoundPool soundPool;
private HashMap<Integer,Integer> soundPoolMap;
public void initSounds(){//初始化聲音的方法
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);//初始化SoundPool
soundPoolMap = new HashMap<Integer, Integer>();//初始化 HashMap
soundPoolMap.put(1, soundPool.load(MainActivity.this, R.raw.welcome1, 1));
}
public void playSound(int sound) {//播放聲音的方法
AudioManager mgr = (AudioManager)MainActivity.this.getSystemService(CoNtext.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//設置最大音量
float volume = streamVolumeCurrent/streamVolumeMax; //設備的音量
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);//播放
}
全站熱搜
留言列表