android上播放按键声音方法

来源:互联网 发布:360免费wifi网络不稳定 编辑:程序博客网 时间:2024/06/06 00:23

在自定义控件时,有时候需要针对用户的操作播放一些声音,一般这些声音格式都是ogg/mp3等格式,目前发现有三种方法来进行播放。

1. 使用playSoundEffect函数进行播放系统内置声音,比如:

?
playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT); //播放向左导航声音

2. 使用AudioManager进行播放系统内置声音,比如:

?
AudioManager mAudioMgr = (AudioManager) context.getSystemService(mContext.AUDIO_SERVICE);mAudioMgr.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);

3. 使用SoundPool播放制定的声音文件,比如:

// 第一个参数为同时播放数据流的最大个数,第二数据流类型,第三为声音质量SoundPool sp = newSoundPool(10, AudioManager.STREAM_SYSTEM, 5);intmusic = sp.load(context, R.raw.key_tick, 1);sp.play(music,1,1,0,0,1);

当然,我们也可以用MediaPlayer进行声音的播放,但是用这个仅仅播放一个按键声音感觉就有点笨重了。

1 0
原创粉丝点击