Android中SoundPool的使用

来源:互联网 发布:教务系统数据库 编辑:程序博客网 时间:2024/05/16 20:52
      在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:

package com.example.soundpool;import java.util.HashMap;import android.media.AudioManager;import android.media.SoundPool;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SPActivity extends Activity {private Button playBtn;private Button pauseBtn;private SoundPool sp;private HashMap<Integer,Integer> spMap;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_sp);playBtn=(Button)findViewById(R.id.button1);pauseBtn=(Button)findViewById(R.id.button2);sp=new SoundPool(2,AudioManager.STREAM_MUSIC,0);spMap = new HashMap<Integer,Integer>();spMap.put(1, sp.load(this, R.raw.sound1, 1));playBtn.setOnClickListener(new OnClickListener() {                    public void onClick(View v) {                // TODO Auto-generated method stub                playSounds(1,1);            }        });pauseBtn.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                // TODO Auto-generated method stub                sp.pause(spMap.get(1));            }        });        }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_sp, menu);return true;}public void playSounds(int sound, int number){        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, number, 1);    }}

 布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="42dp"        android:text="Play" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button1"        android:layout_alignBottom="@+id/button1"        android:layout_centerHorizontal="true"        android:text="Pouse" /></RelativeLayout>


遇到的一个小问题:

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

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

原创粉丝点击