安卓点击事件音效(增加+控制)

项目中遇到的问题,跟大家分享
开发环境:Android Studio
项目中用到的类(关键):SoundPool
项目github地址:https://github.com/zw21544182/BtMusicDemo
项目中所用到的关键知识:
1.基类抽取—(abstract)BaseActivity
2.单例模式—SoundPoolUtil
3.SoundPool类Api运用
最终效果由于涉及到声音就不好贴出来了
先来了解一下SoundPool
构造函数:SoundPool (int maxStreams, int streamType, int srcQuality)
参数解释:

maxStreams streamType srcQuality
最大的流的数量 流的类型建议AudioManager.STREAM_SYSTEM 频的质量,默认是0,目前没有影响

加载音频:load(Context context, int resId, int priority)
参数解释:

context resId priority
上下文对象(不bb) 资源id(用来指定音频文件位置) 优先级:都是短促音频无影响设置为1即可

播放音频:play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
参数解释:

soundID leftVolume rightVolume priority loop rate
音频的序号(load到SoundPool的顺序,从1开始) 左\右声道的音量控制, 0.0 到 1.0 优先级,0是最低优先级 优先级,0是最低优先级 是否循环播放,0为不循环,-1为循环 播放比率,从0.5到2,一般为1,表示正常播放

由于布局比较简单,就不贴代码了
我们可以用单例模式来初始化SoundPool类:具体代码如下:

package zw.btmusicdemo.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import zw.btmusicdemo.R;

/**
 * Created by zhongwang on 2017/9/8.
 */

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;
    }

    private SoundPoolUtil(Context context) {
        soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
        //加载音频文件
        soundPool.load(context, R.raw.music1, 1);
        soundPool.load(context, R.raw.music2, 1);
        soundPool.load(context, R.raw.music3, 1);


    }

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

第二步 抽取基类BaseActivity
1.通过枚举来限制音频播放种类
2.通过abstract让click方法在子类中实现
3.在子类click方法中控制播放哪种音频

package zw.btmusicdemo.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import zw.btmusicdemo.util.SoundPoolUtil;

/**
 * Created by zhongwang on 2017/9/8.
 * 可以设置按钮点击音效的基类activity
 */

public abstract class BaseActivity extends Activity implements View.OnClickListener {
    private SoundPoolUtil soundPoolUtil;
    private int soundId = 0;
    //默认播放 music1
    private MusicType musicType = MusicType.FIRST;

    /**
     * 设置点击按钮音乐类型
     *
     * @param musicType FIRST SECOND THIRD三个参数
     */
    public void setMusicType(MusicType musicType) {
        this.musicType = musicType;
    }

    /**
     * 定义枚举来限定按钮音乐类型
     */
    public enum MusicType {
        FIRST, SECOND, THIRD
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        soundPoolUtil = SoundPoolUtil.getInstance(this);
    }

    /**
     * 在该方法中调用setMusicType来控制点击音乐类型
     * @param v
     */
    public abstract void click(View v);

    @Override
    public void onClick(View v) {
        click(v);
        switch (musicType) {
            case FIRST:
                soundId = 1;
                break;
            case SECOND:
                soundId = 2;
                break;
            case THIRD:
                soundId = 3;
                break;
        }
        soundPoolUtil.play(soundId);
    }

    public void toast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }
}

最后 在子类MainActivity中的运用

package zw.btmusicdemo.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import zw.btmusicdemo.R;
import zw.btmusicdemo.base.BaseActivity;

public class MainActivity extends BaseActivity {
    private Button button1;
    private Button button2;
    private Button button3;

    @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);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void click(View v) {
        switch (v.getId()) {
            case R.id.button1:
                setMusicType(MusicType.FIRST);
                toast("点击了按钮1");
                break;
            case R.id.button2:
                setMusicType(MusicType.SECOND);
                toast("点击了按钮2");
                break;
            case R.id.button3:
                setMusicType(MusicType.THIRD);
                toast("点击了按钮3");
                break;
        }
    }
}

你可能感兴趣的:(安卓点击事件音效(增加+控制))