SoundPool播放音频资源

使用SoundPool播放音频资源,我写在Activity里的一个方法,相关的注释我直接从官网http://developer.android.com/reference/android/media/SoundPool.html里copy了微笑

此文目的就是提供此方法,调用就能直接播放音乐,当然在实际使用还得跟据情况作相应的优化,代码如下:


private void playRightAudio() {
        //maxStreams int: the maximum number of simultaneous streams for this SoundPool object
        //streamType int: the audio stream type as described in AudioManager For example, game applications will normally use STREAM_MUSIC.
        //srcQuality int: the sample-rate converter quality. Currently has no effect. Use 0 for the default.
        SoundPool soundPool=new SoundPool(1, AudioManager.STREAM_MUSIC,0);

        //priority int: the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
        soundPool.load(this,R.raw.right,1);//this是因为写在代码里的一段方法,R.raw.right是指/res/raw包下的相关资源

    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                //soundID       int: a soundID returned by the load() function
                //leftVolume    float: left volume value (range = 0.0 to 1.0)
                //rightVolume   float: right volume value (range = 0.0 to 1.0)
                //priority      int: stream priority (0 = lowest priority)
                //loop          int: loop mode (0 = no loop, -1 = loop forever)
                //rate          float: playback rate (1.0 = normal playback, range 0.5 to 2.0)
                soundPool.play(sampleId,1.0f,1.0f,1,0,1.0f);
            }
        });


        //Release the SoundPool resources. Release all memory and native resources used by the SoundPool object.
        //The SoundPool can no longer be used and the reference should be set to null.
        //在实际使用中要注意在合适的时候调用release释放资源,或在onDestroy调用
//        soundPool.release();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值