zoukankan      html  css  js  c++  java
  • Android平台游戏声音播放实践

    大部分游戏都有音效,不然游戏乐趣会降低很多,而几乎所有音效都是重复播放的。
    下面的代码只加载一次音频文件,但是却可以被多次使用。请将音频文件放置在/res/raw路径中。

    public static final int SOUND_EXPLOSION = 1;
    private SoundPool soundPool;
    private HashMap<Integer, Integer>soundPoolMap;

    private void initSounds() {
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer,Integer>();
        soundPoolMap.put(SOUND_EXPLOSION, soundPool.load(getContext(),R.raw.explosion, 1));
    }

    public void playSound(int sound) {
       
        AudioManagermgr =(AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
        floatstreamVolumeCurrent =mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        floatstreamVolumeMax =mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);   
        float volume= streamVolumeCurrent / streamVolumeMax;

       
       soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0,1f);    
    }

     

    public void explode() {
       playSound(SOUND_EXPLOSION);
    }

  • 相关阅读:
    练习5-3 数字金字塔 (15分)
    JSTL标签
    ssm+mysql+jsp打造在线考试系统WeKnow-学生端
    JSP常用内置对象
    mybatis入门2
    mybtis入门
    数据源的作用
    ssm动态查询向前台传json
    ssm中的注解
    ssm中的模糊查询
  • 原文地址:https://www.cnblogs.com/ganzhijie/p/1804290.html
Copyright © 2011-2022 走看看