Android中SoundPool的使用


在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高、延迟时间较长、不支持多个音频同时播放等。这些缺点决定了MediaPlayer在某些场合的使用情况不会很理想,例如在对时间精准度要求相对较高的游戏开发中。

在游戏开发中我们经常需要播放一些游戏音效(比如:子弹爆炸,物体撞击等),这些音效的共同特点是短促、密集、延迟程度小。在这样的场景下,我们可以使用SoundPool代替MediaPlayer来播放这些音效。

SoundPool的使用步骤是 :

1.在res中新建raw文件夹,然后将需要播放的音频放入其中;

2.初始化SoundPool实例;

3.调用SoundPool的play函数进行播放。

几个重要的函数:

soundPool的构造函数

public SoundPool (int maxStreams, int streamType, int srcQuality)

参数:
maxStreams 同时最大播放音频个数
streamType 音频流的类型,通常使用 STREAM_MUSIC.
srcQuality the sample-rate converter quality. Currently has no effect. Use 0 for the default.

音效播放函数:
public void playSounds(int sound, int number){
    //实例化AudioManager对象,控制声音
    AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
    //最大音量
    float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    //当前音量
    float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
    float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
    //播放
    sp.play(spMap.get(sound),    //声音资源
        volumnRatio,        //左声道
        volumnRatio,        //右声道
        1,            //优先级,0最低
        number,        //循环次数,0是不循环,-1是永远循环
        1);            //回放速度,0.5-2.0之间。1为正常速度
}

代码清单

主Activity:

  1. package com.example.soundpool; 
  2.  
  3. import java.util.HashMap; 
  4.  
  5. import android.media.AudioManager; 
  6. import android.media.SoundPool; 
  7. import android.os.Bundle; 
  8. import android.app.Activity; 
  9. import android.view.Menu; 
  10. import android.view.View; 
  11. import android.view.View.OnClickListener; 
  12. import android.widget.Button; 
  13.  
  14. public class SPActivity extends Activity { 
  15.     private Button playBtn; 
  16.     private Button pauseBtn; 
  17.     private SoundPool sp; 
  18.     private HashMap<Integer,Integer> spMap; 
  19.     @Override 
  20.     public void onCreate(Bundle savedInstanceState) { 
  21.         super.onCreate(savedInstanceState); 
  22.         setContentView(R.layout.activity_sp); 
  23.         playBtn=(Button)findViewById(R.id.button1); 
  24.         pauseBtn=(Button)findViewById(R.id.button2); 
  25.         sp=new SoundPool(2,AudioManager.STREAM_MUSIC,0); 
  26.         spMap = new HashMap<Integer,Integer>(); 
  27.         spMap.put(1, sp.load(this, R.raw.sound1, 1)); 
  28.         playBtn.setOnClickListener(new OnClickListener() {         
  29.             public void onClick(View v) { 
  30.                 // TODO Auto-generated method stub  
  31.                 playSounds(1,1); 
  32.             } 
  33.         }); 
  34.         pauseBtn.setOnClickListener(new OnClickListener() { 
  35.              
  36.             @Override 
  37.             public void onClick(View v) { 
  38.                 // TODO Auto-generated method stub  
  39.                 sp.pause(spMap.get(1)); 
  40.             } 
  41.         });         
  42.     } 
  43.  
  44.     @Override 
  45.     public boolean onCreateOptionsMenu(Menu menu) { 
  46.         getMenuInflater().inflate(R.menu.activity_sp, menu); 
  47.         return true
  48.     } 
  49.     public void playSounds(int sound, int number){ 
  50.         AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE); 
  51.         float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
  52.         float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC); 
  53.         float volumnRatio = audioCurrentVolumn/audioMaxVolumn; 
  54.          
  55.         sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1); 
  56.     } 

布局代码:

  1. <RelativeLayout 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.  
  6.     <Button 
  7.         android:id="@+id/button1" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:layout_alignLeft="@+id/textView1" 
  11.         android:layout_below="@+id/textView1" 
  12.         android:layout_marginTop="42dp" 
  13.         android:text="Play" /> 
  14.  
  15.     <Button 
  16.         android:id="@+id/button2" 
  17.         android:layout_width="wrap_content" 
  18.         android:layout_height="wrap_content" 
  19.         android:layout_alignBaseline="@+id/button1" 
  20.         android:layout_alignBottom="@+id/button1" 
  21.         android:layout_centerHorizontal="true" 
  22.         android:text="Pouse" /> 
  23.  
  24. </RelativeLayout> 

遇到的一个小问题:

pause按钮在第一次播放的时候好用,第一次之后的播放就不管用了,网上找到的解释:

原来这个流对应的ID是需要play方法返回的,后来我用mPresentPlayId存储play返回的流ID,在stop时将流ID使用mPresentPlayId来替换就没问题了,后来输出了下mPresentPlayId的值,发现这个值第一次是2.第二次是4,以后使用这个方法一定要注意这个问题。

相关内容