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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序