大家在android开发过程中 一定有遇到过 需要自己定义播放一些音效的情况吧。(比如 在成功,或者失败的时候播放一小段音效)
今天大伙的福利来啦, 花了点时间 写了一个音效播放控制类, 希望大伙们能够喜欢。 如有好的改进建议,欢迎给我留言。
package com.example.atest_play_focus_sound;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.widget.Toast;
/**
* 因SoundPool初始化需要一小段时间, 故最好在application中初始化该控制类
* @author snake
*
*/
public class ControlFocusSound {
private static boolean loaded;
private static Context mContext;
private static SoundPool soundPool;
private static Map<String, Integer> soundMap;
private static ControlFocusSound mControlFocusSound;
public static final String BACK = "back";
public static final String BACK_TO_TOP = "back_to_top";
public static final String COMFIRE = "comfire";
public static final String MOVE_DOWN = "move_bottom";
public static final String MOVE_LEFT = "move_left";
public static final String MOVE_RIGHT = "move_right";
public static final String NET_CONNECTED = "net_connected";
public static final String NET_FOUND = "net_found";
public static final String TOP_FLOAT_DISABLED = "top_float_disabled";
public static final String TOP_FLOAT = "top_float";
public static final String PAGE_CHANGE = "top_page_move";
private ControlFocusSound(final Context context){
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
initSound(context);
super.run();
}
}.start();
}
public static ControlFocusSound getInstance(Context context){
if(mControlFocusSound == null){
mControlFocusSound = new ControlFocusSound(context);
}
if(null == mContext){
mContext = context;
}
return mControlFocusSound;
}
public static void playSound(final String action) {
AudioManager amgr = (AudioManager) mContext
.getSystemService(Context.AUDIO_SERVICE);
float volumeCurrent = amgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float volumeMax = amgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = volumeCurrent / volumeMax;
if (loaded) {
soundPool.play(soundMap.get(action), volume, volume, 1, 0, 1f);
}else{
Toast.makeText(mContext, "初始化未完成,请稍等", Toast.LENGTH_SHORT).show();
}
}
private void initSound(Context context) {
soundMap = new HashMap<String, Integer>();
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundMap.put(BACK, soundPool.load(context, R.raw.back, 1));
soundMap.put(BACK_TO_TOP,
soundPool.load(context, R.raw.back_to_top, 1));
soundMap.put(COMFIRE,
soundPool.load(context, R.raw.comfire, 1));
soundMap.put(MOVE_DOWN,
soundPool.load(context, R.raw.move_bottom, 1));
soundMap.put(MOVE_LEFT,
soundPool.load(context, R.raw.move_left, 1));
soundMap.put(MOVE_RIGHT,
soundPool.load(context, R.raw.move_right, 1));
soundMap.put(NET_CONNECTED,
soundPool.load(context, R.raw.net_connected, 1));
soundMap.put(NET_FOUND,
soundPool.load(context, R.raw.net_found, 1));
soundMap.put(TOP_FLOAT_DISABLED,
soundPool.load(context, R.raw.top_float_disabled, 1));
soundMap.put(TOP_FLOAT,
soundPool.load(context, R.raw.top_float, 1));
soundMap.put(PAGE_CHANGE,
soundPool.load(context, R.raw.page_change, 1));
}
}