使用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(); }