android播放音效

方法说明

构造方法   

       SoundPool(int maxStreams, int streamType, int srcQuality)
  maxStreams:该参数用于设置同时能够播放多少音效,如设置为3,则最多同时可以播放3个音效
  streamType:音频类型,在游戏中通常设置为STREAM_MUSIC
  srcQuality:音频文件的质量,默认设置为0
  加载音频文件public int load(Context context, int resId, int priority)
  context:应用程序上下文,即调用它的Activity
  resId:资源id
  priority:优先级,可以设置为1

播放音效
    public final int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
  soundID:播放的音乐ID
  leftVolume:用来控制左声道音量
  rightVolume:用来控制右声道音量
  priority:优先级,0最低
  loop:循环次数,0为不循环,-1为永远循环
  rate:回放速度,该值在0.5-2.0之间,1为正常速度

暂定播放
     public final void pause(int streamID)
  streamID音效ID

----------------------------------------------

HashMap soundPoolMap;

private SoundPool sp;

//在onCreate方法里调用

void initSound() {
soundPoolMap = new HashMap();
sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPoolMap.put(1, sp.load(this, R.raw.di, 1));
}

//选择要播放那个声音(count)
public void playSounds(int count) {
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(soundPoolMap.get(count), audioCurrentVolumn, audioCurrentVolumn, 1, 0, (float) 2.0);

}

------------------------------------

 //实例化AudioManager对象,控制声音
    AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
    //最大音量
    float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    //当前音量
    float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);

你可能感兴趣的:(安卓开发)