SoundPool简单使用

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值