sp = new SoundPool( maxStreams, //该参数为设置同时能够播放多少音效 streamType, //该参数设置音频类型 srcQuality //该参数设置音频文件的质量,目前还没有效果,设置为0(默认值) );
加载音频文件可用
public int load (Context context, int resId, int priority)
,下面将介绍方法中各参数的作用。
● context: 应用程序的上下文,即当前的Activity,可以理解为谁来调用这个方法。
● resId:该参数为给出资源的ID。
● priority:优先级,现在还没有作用,设置为1,以便和未来的方法兼容。
sp.load(context, resId, priority)
package wyf.zcl; import java.util.HashMap; import com.example.sample3_1.R; 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 { //Activity创建时调用 SoundPool sp; //得到一个声音池引用 HashMap<Integer,Integer> spMap; //得到一个map的引用 Button b1; //声音播放控制按钮 Button b1Pause; //声音暂停控制按钮 Button b2; //声音播放控制按钮 Button b2Pause; //声音暂停控制按钮 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub playSound(1, 1); //播放第一首音效,循环一遍 Toast.makeText(MyActivity.this, "播放音效1", Toast.LENGTH_SHORT).show(); } }); b1Pause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub playSound(2, 1); //播放第二首音效,循环一遍 Toast.makeText(MyActivity.this, "播放音效2", Toast.LENGTH_SHORT).show(); } }); b2Pause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub sp.pause(spMap.get(2)); Toast.makeText(MyActivity.this, "暂停音效2", Toast.LENGTH_SHORT).show(); } }); } public void initSoundPool(){ //初始化声音池 sp = new SoundPool( 5, //该参数为设置同时能够播放多少音效 AudioManager.STREAM_MUSIC, //该参数设置音频类型 0 //该参数设置音频文件的质量,目前还没有效果,设置为0(默认值) ); spMap = new HashMap<Integer, Integer>(); spMap.put(0, sp.load(this, R.raw.attack02, 1)); spMap.put(1, sp.load(this, R.raw.attack14, 1)); } @SuppressWarnings("static-access") public void playSound(int sound,int number){ AudioManager am = (AudioManager) getSystemService(this.AUDIO_SERVICE);//实例化AudioManager对象 float audioMaxVolumn = //返回当前AudioManager对象的最大音量值 am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float audioVolumn = //返回当前AudioManager对象的音量值 am.getStreamVolume(AudioManager.STREAM_MUSIC); float volumnRatio = audioMaxVolumn/audioVolumn; sp.play( sound, //播放的音乐ID volumnRatio, //左声道音量 volumnRatio, //右声道音量 1, //优先级,0为最低 number, //循环次数,0无不循环,-1无永远循环 1); //回放速度,该值在0.5-2.0之间,1为正常速度. } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <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>
因为gen和src的包名不一致导致错误,所以要修改清单文件的包名
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 { //Activity创建时调用 SoundPool sp; //得到一个声音池引用 HashMap<Integer,Integer> spMap; //得到一个map的引用 Button b1; //声音播放控制按钮 Button b1Pause; //声音暂停控制按钮 Button b2; //声音播放控制按钮 Button b2Pause; //声音暂停控制按钮 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub playSound(1, 1); //播放第一首音效,循环一遍 Toast.makeText(MyActivity.this, "播放音效1", Toast.LENGTH_SHORT).show(); } }); b1Pause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub playSound(2, 1); //播放第二首音效,循环一遍 Toast.makeText(MyActivity.this, "播放音效2", Toast.LENGTH_SHORT).show(); } }); b2Pause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub sp.pause(spMap.get(2)); Toast.makeText(MyActivity.this, "暂停音效2", Toast.LENGTH_SHORT).show(); } }); } public void initSoundPool(){ //初始化声音池 sp = new SoundPool( 5, //该参数为设置同时能够播放多少音效 AudioManager.STREAM_MUSIC, //该参数设置音频类型 0 //该参数设置音频文件的质量,目前还没有效果,设置为0(默认值) ); spMap = new HashMap<Integer, Integer>(); spMap.put(0, sp.load(this, R.raw.attack02, 1)); spMap.put(1, sp.load(this, R.raw.attack14, 1)); } @SuppressWarnings("static-access") public void playSound(int sound,int number){ AudioManager am = (AudioManager) getSystemService(this.AUDIO_SERVICE);//实例化AudioManager对象 float audioMaxVolumn = //返回当前AudioManager对象的最大音量值 am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float audioVolumn = //返回当前AudioManager对象的音量值 am.getStreamVolume(AudioManager.STREAM_MUSIC); float volumnRatio = audioMaxVolumn/audioVolumn; sp.play( sound, //播放的音乐ID volumnRatio, //左声道音量 volumnRatio, //右声道音量 1, //优先级,0为最低 number, //循环次数,0无不循环,-1无永远循环 1); //回放速度,该值在0.5-2.0之间,1为正常速度. } }
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="wyf.zcl" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MyActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <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>