81.android 简单的设置点击音效

//第一步 在res下新建raw,把资源文件click_music.wav放进去

//找点击音效这个网址上面有很多:http://sc.chinaz.com/tag_yinxiao/DianJi.html

//第二步建立单例类:SoundPoolUtil

public class SoundPoolUtil {
    private static SoundPoolUtil soundPoolUtil;
    private SoundPool soundPool;

    //单例模式
    public static SoundPoolUtil getInstance(Context context) {
        if (soundPoolUtil == null)
            soundPoolUtil = new SoundPoolUtil(context);
        return soundPoolUtil;
    }

    @SuppressLint("NewApi")//这里初始化SoundPool的方法是安卓5.0以后提供的新方式
    private SoundPoolUtil(Context context) {
//        soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
        soundPool = new SoundPool.Builder().build();
        //加载音频文件
        soundPool.load(context, R.raw.click_music, 1);

    }

    public void play(int number) {
        Log.d("tag", "number " + number);
        //播放音频
        soundPool.play(number, 1, 1, 0, 0, 1);
    }
}

//第三步 在Activity里使用:

//先完成初始化,比如在initView()或者initData()方法里:

instance = SoundPoolUtil.getInstance(getContext());

//然后再从按钮上调用:

instance.play(1);

//-----------------------------------------------------------------------完------------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/83061034
81
81!
今日推荐