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. }  


  • 2020-04-02 17:02:25

    vue怎么能像jquery那样获得数据

    有时候我们需要获得动态的元素,但是我们没法直接用vue语法,vue一共了当前组件的对象,我们可以避免使用document.get...之类的。

  • 2020-04-02 21:38:15

    Nginx向ExpressJS转发真实IP地址

    由于服务器配置了Nginx的反向代理,在ExpressJS中无法获取到真实的IP地址。本文就介绍了如何配置Nginx以及ExpressJS使其可以显示用户的真实地址。

  • 2020-04-03 08:53:06

    使用自己的QQ邮箱发送自动发送邮件

    话说网上发送邮件的代码很多,但是我由于不细心,导致拿别人的代码发送邮件老是失败,今天就说说几个要注意的地方吧!!!

  • 2020-04-03 10:20:20

    Vue 项目性能优化

    Vue 框架通过数据双向绑定和虚拟 DOM 技术,帮我们处理了前端开发中最脏最累的 DOM 操作部分, 我们不再需要去考虑如何操作 DOM 以及如何最高效地操作 DOM;但 Vue 项目中仍然存在项目首屏优化、Webpack 编译配置优化等问题,所以我们仍然需要去关注 Vue 项目性能方面的优化,使项目具有更高效的性能、更好的用户体验。本文是作者通过实际项目的优化实践进行总结而来,希望读者读完本文,有一定的启发思考,从而对自己的项目进行优化起到帮助。本文内容分为以下三部分组成:

  • 2020-04-03 13:07:46

    flex布局与position:absolute/fixed的冲突问题

    导航栏内,平均分为四块,为了适配各种移动设备,使用了flex布局。 与此同时,产品经理要求:页面上滚越过封面图时,导航栏变为固定定位,浮在页面顶部。 拿到需求之后,思路就是先搞好布局,然后监听window.onscroll,当页面滚的距离大于封面图的时候,给ul加入position:fixed。

  • 2020-04-03 16:56:59

    Inkscape教程

    本教程演示了Inkscape基础使用。这是常规Inkscape文档,你可以预览、编辑、复制、保存。 本教程包括画布导航、管理文档、形状工具基础、选择技术、使用选择转换对象、分组、设置填充和画笔、对齐和Z顺序。有关更高级的主题请查看帮助菜单中的其它教程。