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);
    
    
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程