Android中SoundPool提示音的使用(总结)

SoundPool一般用于系统提示音,比如:滴滴,Duang,Hello,酷狗之类的急促而且短暂的音效。

直接上代码:

package com.deepreality.soundpooldemo;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnPlaySound1, btnPlaySound2, btnPlaySound3, btnPlaySound4, btnPlaySound5, btnRelease;
    private SoundPool mSoundPool;
    private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindViews();
        viewAddListener();
        soundPoolInit();
    }

    private void bindViews() {
        btnPlaySound1 = findViewById(R.id.main_btnPlaySound1);
        btnPlaySound2 = findViewById(R.id.main_btnPlaySound2);
        btnPlaySound3 = findViewById(R.id.main_btnPlaySound3);
        btnPlaySound4 = findViewById(R.id.main_btnPlaySound4);
        btnPlaySound5 = findViewById(R.id.main_btnPlaySound5);
        btnRelease = findViewById(R.id.main_btnRelease);
    }

    private void viewAddListener() {
        btnPlaySound1.setOnClickListener(this);
        btnPlaySound2.setOnClickListener(this);
        btnPlaySound3.setOnClickListener(this);
        btnPlaySound4.setOnClickListener(this);
        btnPlaySound5.setOnClickListener(this);
        btnRelease.setOnClickListener(this);
    }

    private void soundPoolInit() {
        //设置最多可容纳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(this, R.raw.biaobiao, 1));
        soundID.put(3, mSoundPool.load(this, R.raw.duang, 1));
        soundID.put(4, mSoundPool.load(this, R.raw.biaobiao, 1));
        soundID.put(5, mSoundPool.load(this, R.raw.duang, 1));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_btnPlaySound1: {
                mSoundPool.play(soundID.get(1), 1, 1, 0, 0, 1);
                break;
            }
            case R.id.main_btnPlaySound2: {
                mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
                break;
            }
            case R.id.main_btnPlaySound3: {
                mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1);
                break;
            }
            case R.id.main_btnPlaySound4: {
                mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1);
                break;
            }
            case R.id.main_btnPlaySound5: {
                mSoundPool.play(soundID.get(5), 1, 1, 0, 0, 1);
                break;
            }
            case R.id.main_btnRelease: {
                //资源回收
                mSoundPool.release();
                break;
            }
            default:break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/lpCrazyBoy/article/details/81484210
今日推荐