SoundPool简单使用

来源:互联网 发布:node req.session 编辑:程序博客网 时间:2024/05/17 08:34
public class SoundPoolAct extends Activity {    private SoundPool mSoundPool;// SoundPool适合播放短促的声音,如手机按键声音,短促铃声等    private int soundId;    private Button play;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_sound_pool);        play = (Button) findViewById(R.id.play);        // 第一步:新建SoundPool对象:public SoundPool (int maxStreams, int streamType, int srcQuality)        mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);// This constructor was deprecated in API level 21. use SoundPool.Builder instead to create and configure a SoundPool instance        参数说明:        maxStream —— 同时播放的流的最大数量        streamType —— 流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出)        srcQuality —— 采样率转化质量,当前无效果,使用0作为默认值        //第二步:加载声音源文件,共有四种方式,返回的都是对应的sound ID,使用这个id来操作对应的声音        1public int load (AssetFileDescriptor afd, int priority)        2,public int load (Context context, int resId, int priority)        3,public int load (String path, int priority)        4,public int load (FileDescriptor fd, long offset, long length, int priority)        soundId = mSoundPool.load(this, R.raw.music, 1);// 第三个参数priority参数目前未使用,建议设置为1。        // 第三步:播放操作:public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)        play.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View paramView) {                mSoundPool.play(soundId, 1, 1, 0, -1, 1);                参数说明:        soundID a soundID returned by the load() function要播放的声音id        leftVolume  left volume value (range = 0.0 to 1.0)左音量        rightVolume right volume value (range = 0.0 to 1.0)右音量        priority    stream priority (0 = lowest priority)流的优先级,值越大优先级高        loop    loop mode (0 = no loop, -1 = loop forever)循环播放的次数,0为值播放一次,-1为无限循环        rate    playback rate (1.0 = normal playback, range 0.5 to 2.0)播放的速率            }        });    }}

其它常用方法:
1.暂停操作:public final void pause (int streamID)
2.释放资源:public final void release ()
3.设置播放循环模式:public final void setLoop (int streamID, int loop)
4.停止播放:public final void stop (int streamID)

0 0
原创粉丝点击