Android频繁播放简短音频解决方案---SoundPool

本人小菜,昨天接到这样的一个需求,我们有一个智能书柜,用户按密码开锁,开锁的时候按数字几就要播放数字几的发音,我当时就想到了MediaPlayer可以搞,没错,是可以,但是用着用着就出来了一点小遐思,老板肯定不同意啊,于是百度好久终于找到了一个据说已经废弃的了SoundPool,简单的了解了一下SoundPool,他和MediaPlayer来比 就好像一个是轻量级一个是重量级的东西,他没有MediaPlayer播放的延迟,也没有加载的等待,更没有资源的过大,但是,之前我们也说了 他是属于一个轻量级的东西,因为他只适合播放小音频文件,比如按键音效或者游戏音效等等,另外SoundPool推举的语音格式是OGG,但是对于MP3也支持,但是为了安全起见,我们还是用OGG吧,把资源放到res-raw目录下


好了 废话不多说 开撸

首先建立一个SoundPool对象

// SoundPool对象
public static SoundPool mSound = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
SoundPool有三个参数 分别代表

maxstreams 这个SoundPool对象的最大数量的同步流
streamtype 音频流式在audiomanager为例介绍,游戏应用中通常会使用STREAM_MUSIC
srcquality 采样率转换器的质量。目前有没有影响。使用0的默认。
来自官方的翻译: http://www.android-doc.com/reference/android/media/SoundPool.html


接下来写添加音频文件的方法

/**
 * 初始化音频文件
 */
private void initMusic(){
    //分别加入到SoundPool中
    mSound.load(this, R.raw.b0, 1);// 1
    mSound.load(this, R.raw.b1, 1);// 2
    mSound.load(this, R.raw.b2, 1);// 3
    mSound.load(this, R.raw.b3, 1);// 4
    mSound.load(this, R.raw.b4, 1);// 5
    mSound.load(this, R.raw.b5, 1);// 6
    mSound.load(this, R.raw.b7, 1);// 8
    mSound.load(this, R.raw.b8, 1);// 9
    mSound.load(this, R.raw.b9, 1);// 10
}

在onCreate中初始化此方法

下面我们来写播放音频的方法

/**
 * 播放MP3资源
 * @param resId 资源ID
 */
private void StartMusic(int resId){
    /**
     * 第一个参数为播放音频ID
     * 第二个 第三个为音量
     * 第四个为优先级
     * 第五个为是否循环播放
     * 第六个设置播放速度
     * 返回值 不为0即代表成功
     */
    int type = mSound.play(resId, 15, 15, 0, 0, 1);
}


下面来贴一下整体代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    // SoundPool对象
    public static SoundPool mSound = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button_0).setOnClickListener(this);
        findViewById(R.id.button_1).setOnClickListener(this);
        findViewById(R.id.button_2).setOnClickListener(this);
        findViewById(R.id.button_3).setOnClickListener(this);
        findViewById(R.id.button_4).setOnClickListener(this);
        findViewById(R.id.button_5).setOnClickListener(this);
        findViewById(R.id.button_6).setOnClickListener(this);
        findViewById(R.id.button_7).setOnClickListener(this);
        findViewById(R.id.button_8).setOnClickListener(this);
        findViewById(R.id.button_9).setOnClickListener(this);
        initMusic();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_0:
                StartMusic(1);
                break;
            case R.id.button_1:
                StartMusic(2);
                break;
            case R.id.button_2:
                StartMusic(3);
                break;
            case R.id.button_3:
                StartMusic(4);
                break;
            case R.id.button_4:
                StartMusic(5);
                break;
            case R.id.button_5:
                StartMusic(6);
                break;
            case R.id.button_6:
                StartMusic(7);
                break;
            case R.id.button_7:
                StartMusic(8);
                break;
            case R.id.button_8:
                StartMusic(9);
                break;
            case R.id.button_9:
                StartMusic(10);
                break;
        }
    }

    /**
     * 播放MP3资源
     * @param resId 资源ID
     */
    private void StartMusic(int resId){
        /**
         * 第一个参数为播放音频ID
         * 第二个 第三个为音量
         * 第四个为优先级
         * 第五个为是否循环播放
         * 第六个设置播放速度
         * 返回值 不为0即代表成功
         */
        int type = mSound.play(resId, 15, 15, 0, 0, 1);
    }

    /**
     * 初始化音频文件
     */
    private void initMusic(){
        //分别加入到SoundPool中
        mSound.load(this, R.raw.b0, 1);// 1
        mSound.load(this, R.raw.b1, 1);// 2
        mSound.load(this, R.raw.b2, 1);// 3
        mSound.load(this, R.raw.b3, 1);// 4
        mSound.load(this, R.raw.b4, 1);// 5
        mSound.load(this, R.raw.b5, 1);// 6
        mSound.load(this, R.raw.b7, 1);// 8
        mSound.load(this, R.raw.b8, 1);// 9
        mSound.load(this, R.raw.b9, 1);// 10
    }
}

到这里一个简单的小demo就完成了,大家根据自己的需要学习,另外语音合成软件和OGG格式转换软件 我会打包给大家,一起进步

下载地址:http://download.csdn.net/download/crackgmkey/10046337

以下是一个简单的Android播放.wav文件的代码示例: ```java import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { private SoundPool soundPool; private int soundID; boolean loaded = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); // 加载音频文件 soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load(this, R.raw.sound, 1); } public void playSound(View v) { if (loaded) { soundPool.play(soundID, 1, 1, 1, 0, 1f); } } @Override protected void onDestroy() { super.onDestroy(); soundPool.release(); soundPool = null; } } ``` 上述代码使用了Android提供的`SoundPool`类来播放.wav文件。在`onCreate`方法中,首先初始化了`SoundPool`,然后通过`setOnLoadCompleteListener`方法来监听文件是否加载完成,一旦完成就将`loaded`标志设置为`true`。最后调用`load`方法来加载.wav文件。 在`playSound`方法中,如果文件已经加载完成,就可以调用`SoundPool`的`play`方法来播放文件。`play`方法的参数依次为:音频文件的ID、左声道音量(0.0f-1.0f)、右声道音量(0.0f-1.0f)、优先级(0为最低)、循环次数(0表示不循环)、播放速度(1.0f为正常速度)。 最后在`onDestroy`方法中释放`SoundPool`占用的资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值