1、基类抽取—(abstract)BaseActivity
2、单例模式—SoundPoolUtil、
3、SoundPool类Api运用
一、SoundPool:
1、构造函数:SoundPool (int maxStreams, int streamType, int srcQuality)
参数:
2、加载音频:load(Context context, int resId, int priority)
参数:
3、播放音频:play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
参数解释:
4、我们可以用单例模式来初始化SoundPool类:具体代码如下:
package zz.btmusicdemo.util;import android.content.Context;import android.media.AudioManager;import android.media.SoundPool;import android.util.Log;import zz.btmusicdemo.R;/*** Created by abc on /10/16.*/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 zz.btmusicdemo.base;import android.app.Activity;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.View;import android.widget.Toast;import zz.btmusicdemo.util.SoundPoolUtil;/*** Created by abc on /10/16.* 可以设置按钮点击音效的基类activity*/public abstract class BaseActivity extends Activity implements View.OnClickListener {private SoundPoolUtil soundPoolUtil;private int soundId = 0;//默认播放 music1private MusicType musicType = MusicType.FIRST;/*** 设置点击按钮音乐类型** @param musicType FIRST SECOND THIRD三个参数*/public void setMusicType(MusicType musicType) {this.musicType = musicType;}/*** 定义枚举来限定按钮音乐类型*/public enum MusicType {FIRST, SECOND, THIRD}@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);soundPoolUtil = SoundPoolUtil.getInstance(this);}/*** 在该方法中调用setMusicType来控制点击音乐类型* @param v*/public abstract void click(View v);@Overridepublic 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 zz.btmusicdemo.activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import zz.btmusicdemo.R;import zz.btmusicdemo.base.BaseActivity;public class MainActivity extends BaseActivity {private Button button1;private Button button2;private Button button3;@Overrideprotected 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);}@Overridepublic 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;}}}
-END