您的位置:首页 > 移动开发 > Android开发

Android soundpool初探

2013-10-31 20:59 519 查看
内容:本编播客主要讲解一下“即时音效”;

特点:快,短。

在播放这类时间短但是要求反应迅速的的音效,就不能够用不能够使用播放时间较长的音乐播放技术了,而应该采取soundpool技术来播放。

soundpool简介:

该类用于管理和播放应用程序的声音资源,并且将声音文件加载到内存中,出于性能的考虑,一般只将小于7秒左右的声音文件用于该技术进行播放。

下面给出一个播放短促音的例子:(soundpool类的主要注释都放到了该类的代码中)

一、MainActivity.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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
* 游戏中的音效处理
* @author yw-tony
*
*/
public class MyActivity extends Activity implements OnClickListener{
SoundPool sp;                            //得到一个声音池引用
HashMap<Integer,Integer> spMap;            //得到一个map的引用
Button btn_start1,btn_start2,btn_pause1,btn_pause2;                                //声音播放控制按钮
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initSoundPool();//初始化声音池
initViews();
}
private void initViews(){
btn_start1 = (Button)findViewById(R.id.main_start1);
btn_start2 = (Button)findViewById(R.id.main_start2);
btn_pause1 = (Button)findViewById(R.id.main_pause1);
btn_pause2 = (Button)findViewById(R.id.main_pause2);
btn_start1.setOnClickListener(this);
btn_start2.setOnClickListener(this);
btn_pause1.setOnClickListener(this);
btn_pause2.setOnClickListener(this);
}
/**
* 初始化音乐池
*/
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));
}
/**
* 播放短促音
* @param sound 控制播放第一个音乐还是第二个音乐
* @param number 控制音乐播放的循环次数
*/
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为正常速度
);
}
/**
* 按钮的点击事件
*/
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.main_start1:
playSound(1,1);    //播放第一首音效,循环一遍
Toast.makeText(MyActivity.this, "播放音效1", Toast.LENGTH_SHORT).show();
break;
case R.id.main_start2:
playSound(2,1);        //播放第二首音效,循环一遍
Toast.makeText(MyActivity.this, "播放音效2", Toast.LENGTH_SHORT).show();
break;
case R.id.main_pause1:
sp.pause(spMap.get(1));
Toast.makeText(MyActivity.this, "暂停音效1", Toast.LENGTH_SHORT).show();
break;
case R.id.main_pause2:
sp.pause(spMap.get(2));//传入播放音乐的id编号
Toast.makeText(MyActivity.this, "暂停音效2", Toast.LENGTH_SHORT).show();
break;
}

}
}


该类对应的配置文件

<?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/main_start1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="播放音效2"
android:id="@+id/main_start2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="暂停音效1"
android:id="@+id/main_pause1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="暂停音效2"
android:id="@+id/main_pause2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: