Android用SoundPool播放音效

2018-03-11 01:22:39

SoundPool可以用来播放一些较短的音效,如一些信息提醒之类的

先来说下会用到的参数

SoundPool(int maxStreams, int streamType, int srcQuality)的参数

maxStreams:指定支持多少个声音,SoundPool对象中允许同时存在的最大流的数量

streamType:声音类型,流类型可以分为STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING,STREAM_MUSIC 和 STREAM_ALARM四种类型。在AudioManager

定义

srcQuality:声音品质(采样率变换质量),当前无效果,用0作为默认


SoundPool在API 21(Android 5.0)之后就过时了,用SoundPool.Builder

play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

soundID:Load()返回的声音ID号

leftVolume:左声道音量设置

rightVolume:右声道音量设置

priority:播放声音的优先级,数值越高,优先级越大。

loop:是否循环:-1表示无限循环,0表示不循环,其他值表示要重复播放的次数

rate:播放速率:播放速率的取值范围是0.5至2.0,1.0为原始播放速率,2.0的播放速率为原始速率的两位。0.5的播放速率为原始速率的一半。


release()方法释放所有SoundPool对象占据的内存和资源,也可以指定要释放的ID

需要编辑的文件如下


activity_main.xml


[html] view plain copy

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  2.     xmlns:tools="http://schemas.android.com/tools"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:orientation="vertical"  

  6.     tools:context=".MainActivity">  

  7.   

  8.     <Button  

  9.         android:id="@+id/rawPlay"  

  10.         android:layout_width="wrap_content"  

  11.         android:layout_height="wrap_content"  

  12.         android:text="raw" />  

  13.       

  14.     <Button  

  15.         android:id="@+id/assetsPlay"  

  16.         android:layout_width="wrap_content"  

  17.         android:layout_height="wrap_content"  

  18.         android:text="assets" />  

  19.   

  20.     <Button  

  21.         android:id="@+id/btn_release"  

  22.         android:layout_width="wrap_content"  

  23.         android:layout_height="wrap_content"  

  24.         android:text="释放SoundPool" />  

  25.   

  26. </LinearLayout>  




MainActivity.java


[java] view plain copy

  1. package cn.wuxiaocheng.soundpool;  

  2.   

  3.   

  4. import android.content.res.AssetManager;  

  5. import android.media.AudioAttributes;  

  6. import android.media.AudioManager;  

  7. import android.media.SoundPool;  

  8. import android.os.Build;  

  9. import android.os.Bundle;  

  10. import android.support.v7.app.AppCompatActivity;  

  11. import android.view.View;  

  12. import android.view.View.OnClickListener;  

  13. import android.widget.Button;  

  14.   

  15. import java.util.HashMap;  

  16.   

  17. public class MainActivity extends AppCompatActivity implements OnClickListener {  

  18.   

  19.     private Button btn_play1;  

  20.     private Button btn_play2;  

  21.     private Button btn_release;  

  22.     private AssetManager aManager;  

  23.     private SoundPool mSoundPool = null;  

  24.     private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();  

  25.   

  26.     @Override  

  27.     protected void onCreate(Bundle savedInstanceState) {  

  28.         super.onCreate(savedInstanceState);  

  29.         setContentView(R.layout.activity_main);  

  30.         aManager = getAssets();  

  31.         try {  

  32.             initSP();  

  33.         } catch (Exception e) {  

  34.             e.printStackTrace();  

  35.         }  

  36.         bindViews();  

  37.     }  

  38.   

  39.     private void bindViews() {  

  40.         btn_play1 = (Button) findViewById(R.id.rawPlay);  

  41.         btn_play2 = (Button) findViewById(R.id.assetsPlay);  

  42.         btn_release = (Button) findViewById(R.id.btn_release);  

  43.   

  44.         btn_play1.setOnClickListener(this);  

  45.         btn_play2.setOnClickListener(this);  

  46.         btn_release.setOnClickListener(this);  

  47.   

  48.     }  

  49.   

  50.     private void initSP() throws Exception {  

  51.         //当前系统的SDK版本大于等于21(Android 5.0)时  

  52.         if (Build.VERSION.SDK_INT >= 21) {  

  53.             SoundPool.Builder builder = new SoundPool.Builder();  

  54.             //传入音频数量  

  55.             builder.setMaxStreams(2);  

  56.             //AudioAttributes是一个封装音频各种属性的方法  

  57.             AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();  

  58.             //设置音频流的合适的属性  

  59.             attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);  

  60.             //加载一个AudioAttributes  

  61.             builder.setAudioAttributes(attrBuilder.build());  

  62.             mSoundPool = builder.build();  

  63.         }  

  64.         //当系统的SDK版本小于21时  

  65.         else {//设置最多可容纳2个音频流,音频的品质为5  

  66.             mSoundPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);  

  67.         }  

  68.   

  69.         soundID.put(1, mSoundPool.load(this, R.raw.raw, 1));  

  70.         soundID.put(2, mSoundPool.load(getAssets().openFd("assets.mp3"), 1));  //需要捕获IO异常  

  71.     }  

  72.   

  73.     @Override  

  74.     public void onClick(View v) {  

  75.         switch (v.getId()) {  

  76.             case R.id.rawPlay:  

  77.                 mSoundPool.play(soundID.get(1), 11001);  

  78.                 break;  

  79.             case R.id.assetsPlay:  

  80.                 mSoundPool.play(soundID.get(2), 11001);  

  81.                 break;  

  82.             case R.id.btn_release:  

  83.                 //回收SoundPool资源  

  84.                 mSoundPool.release();  

  85.                 break;  

  86.         }  

  87.     }  

  88. }  


  • 2018-03-21 22:28:02

    Java加密算法 AES

    AES 算法 对称加密,密码学中的高级加密标准 2005年成为有效标准

  • 2018-03-24 13:23:26

    Only the original thread that created a view hierarchy can touch its views

    很多网友在Android中使用多线程处理UI相关内容时可能会发现Logcat提示Only the original thread that created a view hierarchy can touch its views这样的错误,这主要是Android的相关View和控件不是线程安全的,我们必须做独立的处理这点比J2ME麻烦一些,这里Android给 我们提供了很多方法,有关线程的

  • 2018-03-26 18:05:09

    MYSQL OR与AND一起 的用法

    查询结果是id = 2且age = 20或者id=1SELECT * from student WHERE id = 1 or id = 2 AND age = 20;12

  • 2018-03-27 11:27:09

    Java中Set集合的使用

    Set类继承了Conllection类,是一种集合类。Set的实现类有三个,下面我们会一一来说这些的不一样。

  • 2018-03-27 11:36:58

    Java中数组、List、Set互相转换

    需要注意的是, Arrays.asList() 返回一个受指定数组决定的固定大小的列表。所以不能做 add 、 remove等操作,否则会报错。

  • 2018-03-27 16:37:57

    Java 8 将List转换为Map

    几个Java 8示例来向您展示如何将一个List对象转换为一个Map,以及如何处理重复的键

  • 2018-03-31 09:37:33

    Android Sqlite查询优化之一---运用索引

    最近笔者在做聊天功能模块,发现当本地聊天数据记录过大,以10万行数据进行了检索测试,发现时间太长了,要6s左右,但学着运用了下索引,时间大大提升,紧要几百毫秒就能完成. 以下内容,摘抄至网络