zoukankan      html  css  js  c++  java
  • 5月20日学习日志

    今天学习了使用SoundPool播放音效。

    代码为:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        private Button btn_play1;
        private Button btn_play2;
        private Button btn_play3;
        private Button btn_play4;
        private Button btn_play5;
        private Button btn_release;
        private AssetManager aManager;
        private SoundPool mSoundPool = null;
        private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            aManager = getAssets();
            try {
                initSP();
            } catch (Exception e) {
                e.printStackTrace();
            }
            bindViews();
        }
    
        private void bindViews() {
            btn_play1 = (Button) findViewById(R.id.btn_play1);
            btn_play2 = (Button) findViewById(R.id.btn_play2);
            btn_play3 = (Button) findViewById(R.id.btn_play3);
            btn_play4 = (Button) findViewById(R.id.btn_play4);
            btn_play5 = (Button) findViewById(R.id.btn_play5);
            btn_release = (Button) findViewById(R.id.btn_release);
    
            btn_play1.setOnClickListener(this);
            btn_play2.setOnClickListener(this);
            btn_play3.setOnClickListener(this);
            btn_play4.setOnClickListener(this);
            btn_play5.setOnClickListener(this);
            btn_release.setOnClickListener(this);
    
        }
    
        private void initSP() throws Exception{
            //设置最多可容纳5个音频流,音频的品质为5
            mSoundPool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 5);
            soundID.put(1, mSoundPool.load(this, R.raw.duang, 1));
            soundID.put(2 , mSoundPool.load(getAssets().openFd("biaobiao.mp3") , 1));  //需要捕获IO异常
            soundID.put(3, mSoundPool.load(this, R.raw.duang, 1));
            soundID.put(4, mSoundPool.load(this, R.raw.duang, 1));
            soundID.put(5, mSoundPool.load(this, R.raw.duang, 1));
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_play1:
                    mSoundPool.play(soundID.get(1), 1, 1, 0, 0, 1);
                    break;
                case R.id.btn_play2:
                    mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
                    break;
                case R.id.btn_play3:
                    mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1);
                    break;
                case R.id.btn_play4:
                    mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1);
                    break;
                case R.id.btn_play5:
                    mSoundPool.play(soundID.get(5), 1, 1, 0, 0, 1);
                    break;
                case R.id.btn_release:
                    mSoundPool.release();   //回收SoundPool资源
                    break;
            }
        }
    }
  • 相关阅读:
    用elasticsearch分析中国大学省份分布
    【翻译】Kinect v1和Kinect v2的彻底比较
    翻译 Tri-Ace:在Shader里近似渲染公式
    翻译 基于物理渲染的美术资源设计流程
    翻译 次世代基于物理渲染的反射模型
    关于Depth Bounds Test (DBT)和在CE3的运用
    使用Xcode GPU Frame Caputre教程
    如何使用Xcode分析调试在真机运行的UE4 IOS版游戏
    个人翻译的cedec2010基于物理的光照
    使用Nsight查找CE3的渲染bug
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910667.html
Copyright © 2011-2022 走看看