android 自定义音效播放

   大家在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));
 }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值