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-03-18 21:30:57

    基于OpenLayers实现地理围栏 谷歌百度高德地图都可以

    前言.因为项目有点特殊,需要接入谷歌地图实现地理围栏,因为谷歌地图的地理围栏接口相关接口并不完善,于是就换了一个思路,利用OpenLayers来实现地理围栏 openlayers 中文地址 http://weilin.me/ol3-primer/ch02/index.html 作者:zcty0701 链接:https://www.jianshu.com/p/60e88ee1e843 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 2020-03-19 17:12:40

    百度地图放大覆盖物消失

    产生问题的原因是因为我们用的普通的点数组生成的多边形,我们应该用百度的点数组生成就没问题了。

  • 2020-03-19 19:15:47

    vue中methods watch和compute的区别和联系

    首先要说,methods,watch和computed都是以函数为基础的,但各自却都不同 而从作用机制和性质上看,methods和watch/computed不太一样,所以我接下来的介绍主要有两个对比: 1.methods和(watch/computed)的对比

  • 2020-03-19 19:50:31

    用vue做的跟随鼠标移动的div

    随鼠标移动的动画效果,之前一直使用angular和react,没怎么接触过vue,先做一个vue的简单例子,然后再整合。

  • 2020-03-20 13:35:55

    随便想到,群聊天的数据库简单设计

    拆分成两个表,一个是消息的流水表,一个是每个人的配置表。 记录每个群下面的这个用户的最后读取的消息last_msg_id,然后在计算消息未读数据。 这样优化之后数据将减少好多,数量是 m+n条数据。不在是成倍增长了。

  • 2020-03-20 13:39:50

    类似与微信朋友圈功能数据库如何实现

    每次发圈子的时候,给关注我的每个uuid,发一个内容id。 大概表的设计就是 uuid,idlist 这样的,idlist是按照时间顺序的。 然后定期删除idlist过多的老圈子。

  • 2020-03-21 00:11:38

    Android卡片布局(圆角阴影)的几种实现及分析

    在开发中,为了界面美观,圆角view和阴影效果是开发中经常遇到的UI场景,比如银行卡效果,卡片式itemView布局,Banner图等,开发中我们通过各种方式实现了这种效果,但是哪种方案最好呢,接下来本文将比较几种常见的圆角阴影布局实现,并从内存占用角度分析它们的优缺点.