实现Android播放声音资源的一个简单的工具类
2015-08-20 16:20
411 查看
为了调用方便,自己写了一个简单的Android播放声音的工具类,供大家参考。
声音资源存在raw文件夹中,一般支持ogg,wav等格式。
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
/**
* TODO 播放声音工具类
*
* @author Leonardo
* @date 2015-8-20 下午3:25:10
* @since
* @version
*/
@SuppressLint("UseSparseArrays")
@SuppressWarnings("deprecation")
public class SoundUtils {
/**
* TODO 上下文
*/
private Context context;
/**
* TODO 声音池
*/
private SoundPool soundPool;
/**
* TODO 添加的声音资源参数
*/
private HashMap<Integer, Integer> soundPoolMap;
/**
* TODO 声音音量类型,默认为多媒体
*/
private int soundVolType = 3;
/**
* TODO 无限循环播放
*/
public static final int INFINITE_PLAY = -1;
/**
* TODO 单次播放
*/
public static final int SINGLE_PLAY = 0;
/**
* TODO 铃声音量
*/
public static final int RING_SOUND = 2;
/**
* TODO 媒体音量
*/
public static final int MEDIA_SOUND = 3;
/**
*
* TODO 构造器内初始化
*
* @author Leonardo
* @date 2015-8-20 下午4:13:54
* @param context
* 上下文
* @param soundVolType
* 声音音量类型,默认为多媒体
*/
public SoundUtils(Context context, int soundVolType) {
this.context = context;
this.soundVolType = soundVolType;
// 初始化声音池和声音参数map
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
}
/**
*
* TODO 添加声音文件进声音池
*
* @author Leonardo
* @date 2015-8-20 下午3:50:53
* @param order
* 所添加声音的编号,播放的时候指定
* @param soundRes
* 添加声音资源的id
* @see
*/
public void putSound(int order, int soundRes) {
// 上下文,声音资源id,优先级
soundPoolMap.put(order, soundPool.load(context, soundRes, 1));
}
/**
*
* TODO 播放声音
*
* @author Leonardo
* @date 2015-8-20 下午3:52:44
* @param order
* 所添加声音的编号
* @param times
* 循环次数,0无不循环,-1无永远循环
* @see
*/
@SuppressWarnings("static-access")
public void playSound(int order, int times) {
// 实例化AudioManager对象
AudioManager am = (AudioManager) context
.getSystemService(context.AUDIO_SERVICE);
// 返回当前AudioManager对象播放所选声音的类型的最大音量值
float maxVolumn = am.getStreamMaxVolume(soundVolType);
// 返回当前AudioManager对象的音量值
float currentVolumn = am.getStreamVolume(soundVolType);
// 比值
float volumnRatio = currentVolumn / maxVolumn;
soundPool.play(soundPoolMap.get(order), volumnRatio, volumnRatio, 1,
times, 1);
}
/**
* TODO 设置 soundVolType 的值
*/
public void setSoundVolType(int soundVolType) {
this.soundVolType = soundVolType;
}
}
使用的时候先初始化一个声音播放工具
SoundUtils soundUtils = new SoundUtils(this, SoundUtils.RING_SOUND);
参数分别是Context和声音音量类型(受铃声还是多媒体控制)
然后添加声音进去
soundUtils.putSound(0, R.raw.你的声音文件名);参数是添加声音的编号和资源id
需要播放的地方执行这句即可
soundUtils.playSound(0, SoundUtils.SINGLE_PLAY);参数分别是声音的编号和循环次数
谢谢观阅,有问题请指教
声音资源存在raw文件夹中,一般支持ogg,wav等格式。
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
/**
* TODO 播放声音工具类
*
* @author Leonardo
* @date 2015-8-20 下午3:25:10
* @since
* @version
*/
@SuppressLint("UseSparseArrays")
@SuppressWarnings("deprecation")
public class SoundUtils {
/**
* TODO 上下文
*/
private Context context;
/**
* TODO 声音池
*/
private SoundPool soundPool;
/**
* TODO 添加的声音资源参数
*/
private HashMap<Integer, Integer> soundPoolMap;
/**
* TODO 声音音量类型,默认为多媒体
*/
private int soundVolType = 3;
/**
* TODO 无限循环播放
*/
public static final int INFINITE_PLAY = -1;
/**
* TODO 单次播放
*/
public static final int SINGLE_PLAY = 0;
/**
* TODO 铃声音量
*/
public static final int RING_SOUND = 2;
/**
* TODO 媒体音量
*/
public static final int MEDIA_SOUND = 3;
/**
*
* TODO 构造器内初始化
*
* @author Leonardo
* @date 2015-8-20 下午4:13:54
* @param context
* 上下文
* @param soundVolType
* 声音音量类型,默认为多媒体
*/
public SoundUtils(Context context, int soundVolType) {
this.context = context;
this.soundVolType = soundVolType;
// 初始化声音池和声音参数map
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
}
/**
*
* TODO 添加声音文件进声音池
*
* @author Leonardo
* @date 2015-8-20 下午3:50:53
* @param order
* 所添加声音的编号,播放的时候指定
* @param soundRes
* 添加声音资源的id
* @see
*/
public void putSound(int order, int soundRes) {
// 上下文,声音资源id,优先级
soundPoolMap.put(order, soundPool.load(context, soundRes, 1));
}
/**
*
* TODO 播放声音
*
* @author Leonardo
* @date 2015-8-20 下午3:52:44
* @param order
* 所添加声音的编号
* @param times
* 循环次数,0无不循环,-1无永远循环
* @see
*/
@SuppressWarnings("static-access")
public void playSound(int order, int times) {
// 实例化AudioManager对象
AudioManager am = (AudioManager) context
.getSystemService(context.AUDIO_SERVICE);
// 返回当前AudioManager对象播放所选声音的类型的最大音量值
float maxVolumn = am.getStreamMaxVolume(soundVolType);
// 返回当前AudioManager对象的音量值
float currentVolumn = am.getStreamVolume(soundVolType);
// 比值
float volumnRatio = currentVolumn / maxVolumn;
soundPool.play(soundPoolMap.get(order), volumnRatio, volumnRatio, 1,
times, 1);
}
/**
* TODO 设置 soundVolType 的值
*/
public void setSoundVolType(int soundVolType) {
this.soundVolType = soundVolType;
}
}
使用的时候先初始化一个声音播放工具
SoundUtils soundUtils = new SoundUtils(this, SoundUtils.RING_SOUND);
参数分别是Context和声音音量类型(受铃声还是多媒体控制)
然后添加声音进去
soundUtils.putSound(0, R.raw.你的声音文件名);参数是添加声音的编号和资源id
需要播放的地方执行这句即可
soundUtils.playSound(0, SoundUtils.SINGLE_PLAY);参数分别是声音的编号和循环次数
谢谢观阅,有问题请指教
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories