提示音效

https://blog.csdn.net/hellokandy/article/details/79859670

在res下新建一个文件夹raw,把声音文件放在这里面

import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
 
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
 
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
    private Button button1, button2, button3, button4;
    private SoundPool soundpool;
    private Map soundmap = new HashMap();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        button4 = (Button)findViewById(R.id.button4);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
 
        //当前系统的SDK版本大于等于21(Android 5.0)时
        if(Build.VERSION.SDK_INT > 21){
            SoundPool.Builder builder = new SoundPool.Builder();
            //传入音频数量
            builder.setMaxStreams(5);
            //AudioAttributes是一个封装音频各种属性的方法
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            //设置音频流的合适的属性
            attrBuilder.setLegacyStreamType(AudioManager.STREAM_SYSTEM);//STREAM_MUSIC
            //加载一个AudioAttributes
            builder.setAudioAttributes(attrBuilder.build());
            soundpool = builder.build();
        }else{
            soundpool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 0);
        }
        //
        soundmap.put(1, soundpool.load(this, R.raw.chimes, 1));
        soundmap.put(2, soundpool.load(this, R.raw.enter, 1));
        soundmap.put(3, soundpool.load(this, R.raw.notify, 1));
        soundmap.put(4, soundpool.load(this, R.raw.ringout, 1));
        soundmap.put(5, soundpool.load(this, R.raw.ding, 1));
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        soundpool.play(soundmap.get(5), 1, 1, 0, 0, 1);
        //return  true;
        return super.onKeyDown(keyCode, event);
    }
 
    @Override
    protected void onDestroy() {
        soundpool.release();//回收soundpool资源
        super.onDestroy();
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.button1:
                soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1);
                break;
            case R.id.button2:
                soundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);
                break;
            case R.id.button3:
                soundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);
                break;
            case R.id.button4:
                soundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);
                break;
        }
    }
}
————————————————
版权声明:本文为CSDN博主「hellokandy」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hellokandy/article/details/79859670

你可能感兴趣的:(提示音效)