最近想实现一个警告的功能 需要把用户正在播放的声音覆盖掉 或者让其他声音音量降低,我用了MediaPlayer的类来播放 设置了mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); 但是实际没有效果 请大神指教
你是想要警告的时候原来正在播放的声音还要继续的么?那你可以使用安卓的SoundPool类,此类特点就是低延迟播放,适合播放实时音实现同时播放多个声音,如游戏中炸弹的爆炸音等小资源文件,此类音频比较适合放到资源文件夹 res/raw下和程序一起打成APK文件。
用法如下:
SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong1, 1));
soundPoolMap.put(2, soundPool.load(this, R.raw.dingdong2, 2));
public void playSound(int sound, int loop) {
AudioManager mgr = (AudioManager)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, loop, 1f);
//参数:1、Map中取值 2、当前音量 3、最大音量 4、优先级 5、重播次数 6、播放速度
}
this.playSound(1, 0);