先贴上代码
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;@Overridepublic 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>


                                        

   

更多相关文章

  1. 播放记录的SQLite数据库实现
  2. Android_播放器的进度条
  3. android使用opengl es2.0播放视频
  4. Android(安卓)Media Recorder录音播放源代码
  5. ObjectAnimators 动画的暂停与重新开始
  6. Android音乐播放器【支持:速率调节,音调调节,采样率调节】
  7. Android(安卓)编程案例-本地音乐播放器源码及使用注意事项
  8. Android(安卓)WebView 总结 —— 使用HTML5播放视频及全屏方案
  9. vitamio视频播放报错: java.lang.UnsatisfiedLinkError:dalvik.sy

随机推荐

  1. android android:taskAffinity 详解
  2. android 2.1系统自带资源--- 系统图标资
  3. Android(安卓)新手入门(2)-常用控件
  4. Android实战技巧之二十二:Android(安卓)5.
  5. Android(安卓)扩大点击区域
  6. android 2.1系统自带资源--- 系统图标资
  7. 一个 android 资料
  8. ReactNative android离线加载
  9. android布局属性预览
  10. Android字体大小与titile标题居中设置