qq_32750865 2017-02-07 01:30 采纳率: 0%
浏览 1745

关于安卓音频播放怎么覆盖其他声音

最近想实现一个警告的功能 需要把用户正在播放的声音覆盖掉 或者让其他声音音量降低,我用了MediaPlayer的类来播放 设置了mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); 但是实际没有效果 请大神指教

  • 写回答

1条回答

  • 简一_hz 2017-02-07 01:39
    关注

    你是想要警告的时候原来正在播放的声音还要继续的么?那你可以使用安卓的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);
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作