先贴上代码
package com.example.voice;
import java.util.HashMap;
import java.util.Map;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button bt_start_one;
private Button bt_start_two;
private Button bt_pause_one;
private Button bt_pause_two;
private SoundPool sp;
private Map<Integer, Integer> map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initSoundpool();
bt_start_one = (Button) findViewById(R.id.bt_start_one);
bt_start_two = (Button) findViewById(R.id.bt_start_two);
bt_pause_one = (Button) findViewById(R.id.bt_pause_one);
bt_pause_two = (Button) findViewById(R.id.bt_pause_two);
bt_start_one.setOnClickListener(this);
bt_start_two.setOnClickListener(this);
bt_pause_one.setOnClickListener(this);
bt_pause_two.setOnClickListener(this);
}
/**
* 初始化
*/
private void initSoundpool() {
sp = new SoundPool(5,// 同时播放的音效
AudioManager.STREAM_MUSIC, 0);
map = new HashMap<Integer, Integer>();
map.put(1, sp.load(this, R.raw.attack02, 1));
map.put(2, sp.load(this, R.raw.attack14, 1));
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_start_one:
playSound(1, 10);// 播放第一首音效,播放一遍
Toast.makeText(this, "播放第一首音效", 0).show();
break;
case R.id.bt_start_two:
playSound(2, 10);
Toast.makeText(this, "播放第二首音效", 0).show();
break;
case R.id.bt_pause_one:
sp.pause(map.get(1));
Toast.makeText(this, "暂停第一首音效", 0).show();
break;
case R.id.bt_pause_two:
sp.pause(map.get(2));
Toast.makeText(this, "暂停第二首音效", 0).show();
break;
default:
break;
}
}
/**
*
* @param sound 文件
* @param number 循环次数
*/
private void playSound(int sound, int number) {
AudioManager am = (AudioManager) getSystemService(this.AUDIO_SERVICE);// 实例化
float audioMaxVolum = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);// 音效最大值
float audioCurrentVolum = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float audioRatio = audioCurrentVolum / audioMaxVolum;
sp.play(map.get(sound),
audioRatio,// 左声道音量
audioRatio,// 右声道音量
1, // 优先级
number,// 循环播放次数
1);// 回放速度,该值在0.5-2.0之间 1为正常速度
}
}
界面代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_start_one"
android:layout_marginBottom="10dip"
android:text="播放即时音效1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_pause_one"
android:layout_marginBottom="10dip"
android:text="暂停即时音效1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_start_two"
android:layout_marginBottom="10dip"
android:text="播放即时音效2"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_pause_two"
android:layout_marginBottom="10dip"
android:text="暂停即时音效1"
/>
</LinearLayout>