Android 添加音效

Android About 1,312 words

代码

private SoundPool mSoundPool;
private int mMusic;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //已过时,建议用SoundPool.Builder代替
    //第一个参数为同时播放数据流的最大个数,第二数据流类型,第三为声音质量
    mSoundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
    //第1个参数为上下文,声音素材放到res/raw里,第2个参数即为资源文件,第3个参数为音乐的优先级
    mMusic = mSoundPool.load(MainActivity.this, R.raw.shake, 1);
    mBtn.setOnClickListener(listener);
}
private OnClickListener listener =new OnClickListener(){
    @Override
    public void onClick(View view) {
    /**
     * @param soundID a soundID returned by the load() function
     * @param leftVolume left volume value (range = 0.0 to 1.0)
     * @param rightVolume right volume value (range = 0.0 to 1.0)
     * @param priority stream priority (0 = lowest priority)
     * @param loop loop mode (0 = no loop, -1 = loop forever)
     * @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
     * @return non-zero streamID if successful, zero if failed
     */
    public final int play(int soundID, float leftVolume, float rightVolume,int priority, int loop, float rate)

        //开始播放音乐
        mSoundPool.play(mMusic, 1, 1, 0, 0, 1);

        //停止播放音乐
        mSoundPool.stop(mMusic);        
}};
Views: 3,500 · Posted: 2019-04-14

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh