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;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值