音效: 快,短。(持续时间小于
7秒)
实现技术: android.media.SoundPool实现 (管理和播放应用程序的声音资源,直接加载到内存)。
一.基础知识:
1. 创建一个SoundPool :
我们先看看SoundPool函数的定义,如下:
public SoundPool(
int maxStream, // 同时播放的流的最大数量
int streamType,// 流的类型,一般为STREAM_MUSIC
int srcQuality // 采样率转化质量,当前无效果,使用0作为默认值
)
eg.
SoundPool soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
创建了一个最多支持3个流同时播放的,类型标记为音乐的SoundPool。
2. 加载一个音频文件:
以下方法,取其一即可:
int load(Context context, int resId, int priority) //从APK资源载入
int load(FileDescriptor fd, long offset, long length, int priority) //从FileDescriptor对象载入
int load(AssetFileDescriptor afd, int priority) //从Asset对象载入
int load(String path, int priority) //从完整文件路径名载入
我目前使用的是第一种,从APK资源载入:
int load(
Context context, // 应用程序的上下文,即当前的Activity,可理解为谁来调用这个方法。
int resId, // 资源的ID
int priority // 优先级,我们设置为1即可。
)
3. 播放音效:
int play(
int soundID, // 播放的音乐ID
float leftVolume, // 左声道音量
float rightVolume, // 右声道音量
int priority, // 优先级,0为最低
int loop, // 循环次数,0为不循环,-1为永远循环
float rate // 回放速度,该值在0.5-2.0之间,1为正常速度。
)
4. 暂停音效:
void pause(int streamID) // 参数为 音效的ID。
二.编程实现:
1. 界面编辑(
res\layout\main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="播放音效1"
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 添加一个Button控件 -->
<Button
android:text="播放音效2"
android:id="@+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 添加一个Button控件 -->
<Button
android:text="暂停音效1"
android:id="@+id/Button1Pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 添加一个Button控件 -->
<Button
android:text="暂停音效2"
android:id="@+id/Button2Pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button> <!-- 添加一个Button控件 -->
</LinearLayout>
LinearLayout 定义了一个 vertical(垂直方向) 上的线性布局。
在这个布局之内,填充四个按钮控件,其text, id, layout_width, 和layout_height 参数在里面有填充说明。
界面布局效果如下:
2. 代码编辑(\src\wyf\zcl\MyActivity.java):
package wyf.zcl;
import java.util.HashMap; // 引入相关包
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
SoundPool sp; //得到一个声音池引用
HashMap<Integer,Integer> spMap; //得到一个map的引用
Button b1; //声音播放控制按钮
Button b1Pause; //声音暂停控制按钮
Button b2; //声音播放控制按钮
Button b2Pause; //声音暂停控制按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initSoundPool(); //初始化声音池
b1=(Button)findViewById(R.id.Button01); //声音播放控制按钮实例化
b2=(Button)findViewById(R.id.Button02); //声音播放控制按钮实例化
b1Pause=(Button)findViewById(R.id.Button1Pause);
b2Pause=(Button)findViewById(R.id.Button2Pause);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(1,1); //播放第一首音效,循环一遍
Toast.makeText(MyActivity.this, "播放音效1", Toast.LENGTH_SHORT).show();
}});
b1Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.pause(spMap.get(1));
Toast.makeText(MyActivity.this, "暂停音效1", Toast.LENGTH_SHORT).show();
}});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(2,1); //播放第二首音效,循环一遍
Toast.makeText(MyActivity.this, "播放音效2", Toast.LENGTH_SHORT).show();
}});
b2Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sp.pause(spMap.get(2));
Toast.makeText(MyActivity.this, "暂停音效2", Toast.LENGTH_SHORT).show();
}});
}
public void initSoundPool(){ //初始化声音池
sp=new SoundPool(
5, //maxStreams参数,该参数为设置同时能够播放多少音效
AudioManager.STREAM_MUSIC, //streamType参数,该参数设置音频类型,在游戏中通常设置为:STREAM_MUSIC
0 //srcQuality参数,该参数设置音频文件的质量,目前还没有效果,设置为0为默认值。
);
spMap=new HashMap<Integer,Integer>();
spMap.put(1, sp.load(this, R.raw.attack02, 1));
spMap.put(2, sp.load(this, R.raw.attack14, 1));
}
public void playSound(int sound,int number){ //播放声音,参数sound是播放音效的id,参数number是播放音效的次数
AudioManager am=(AudioManager)this.getSystemService(this.AUDIO_SERVICE);//实例化AudioManager对象
float audioMaxVolumn=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //返回当前AudioManager对象的最大音量值
float audioCurrentVolumn=am.getStreamVolume(AudioManager.STREAM_MUSIC);//返回当前AudioManager对象的音量值
float volumnRatio=audioCurrentVolumn/audioMaxVolumn;
sp.play(
spMap.get(sound), //播放的音乐id
volumnRatio, //左声道音量
volumnRatio, //右声道音量
1, //优先级,0为最低
number, //循环次数,0无不循环,-1无永远循环
1 //回放速度 ,该值在0.5-2.0之间,1为正常速度
);
}
}
前排几个import用来导入程序依赖的相关包;
然后是Activity类,相当于注册我们界面的回调函数,@Override了Activity的 onCreate()函数,当创建MyActivity(即我们的窗口时)调用该方法。
initSoundPool()函数 初始化音效池;
findViewById 将按钮控件实例化;
@Override 按钮的按键监听函数 setOnKeyListener()。
=============================================================================================================
Eclipse显示行号: 【Ctrl+F10】-->>【Show Line Numbers】
分享到:
相关推荐
使用 SoundPool 播放音效,SoundPool 还支持自行设置声音的品质、音量、 播放比率等参数
Android游戏设计之-------游戏音效的播放 SoundPool
android背景音乐、背景音效实例,其中大音频音乐播放使用MediaPlayer实现,小音频音效播放使用SoundPool实现
即时音效, 通过android.media.SoundPool实现 具体请参见 http://blog.csdn.net/aduovip/article/details/9026567
Android设备的一些较短的声音可以用SoundPool来做,如消息提醒等,此例子介绍了SoundPool的用法。
android soundpool播放音乐,使用soundpool播放音乐。
如果在程序应用中(比如:游戏的音效等)需要播放密集、短促的音效,这时就使用SoundPool来播放音效,SoundPool使用音效池的概念来管理多个短促的音效,例如它可以开始就10个音效,以后在程序中按音效的ID进行播放。...
Android中的SoundPool一般用于游戏中的多个音频文件中,以后有意向做游戏开发,或对多个音频播放有要求的可以研究下SoundPool。
NULL 博文链接:https://rayln.iteye.com/blog/1224594
Android 应用开发源码 参考与学习使用
Android下使用SoundPool的示例,详细参见博客:http://www.cnblogs.com/plokmju/p/android_SoundPool.html
对于Android播放一些简短音效,例如提示音,或者铃声,相对于使用MediaPlayer,SoundPool可以节省更多资源,并且可以同时播放多个音效,而且可以针对不同音效设置不同播放品质 实现 SoundPool的具体作用,就不再阐述...
代码中使用SoundPool播放多个音效。
资源为一个专门处理短音效soundPool的工具类,使用起来很方便,封装的很好!使用方法: ...2、在开始播放音效的时候调用soundPlay.play(ID_SOUND_LOSE, 0)即可实现短音效的播放。 是不是很简单呢!
Android应用源码之(SoundPool音效).zip项目安卓应用源码下载Android应用源码之(SoundPool音效).zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
我们之前有用过MediaPlayer进行播放音频文件,但是当我们的应用程序需要经常的播放密集、短促的音效时,调用MediaPlayer则会占用系统的大量资源,且延时时间较长,不支持多个音频同时播放。这种简单的音乐的播放就...
本文实例讲述了Android编程实现使用SoundPool播放音乐的方法。分享给大家供大家参考,具体如下: 如果应用程序要播放密集、短促的音效,这时还用MediaPlayer就显得不合适了。MediaPlayer存在如下缺点: 1、资源占用...
在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高、延迟时间较长、不支持多个音频同时播放等。这些缺点决定了MediaPlayer在某些场合的使用情况不会很理想...
Android SoundPool音效使用Demo源码.rar