Looking for material re-doing flappybird today, I learned how to set the background audio for the app, and found that by encapsulating the Soundpool class, you can do it very well.
The Soundpool analogy is suitable for playing some of the more ephemeral and smaller audio streams like game sounds, and it supports playing multiple audio streams at the same time, while larger audio is better suited for MediaPlayer playback.
A general explanation of the basic use of the Soundpool class is as follows:
1. Load the audio resources you want to use through the load () method.
2. Create and pass in the Soundpool.onloadcompletelistener object by creating the Setonloadcompletelistener () method of the Soundpool object. Overload public void Onloadcomplete (Soundpool arg0, int arg1, int arg2) methods to check whether the audio is loaded successfully.
3. play the audio stream by using the play (int soundid, float leftvolume, float rightvolume, int priority, int loop, float rate) method. Soundid is going to The audio number that is played, priority is the playback precedence of the audio, and loop is the number of times the loop is played (-1 is infinite looping). Rate is the playback speed ~
The following is a class for playing audio that is encapsulated when you redo Flappybird. Compared to the practical MediaPlayer. This method of playing sounds is more efficient:
Package Com.example.flappy.util;import Java.util.hashmap;import Android.media.audiomanager;import Android.media.soundpool;import Android.widget.toast;import Com.example.flappy.mainactivity;import com.example.flappy.r;/* * SoundPlayer is responsible for audio playback */public class SoundPlayer {private Soundpool soundpool;private Mainactivity mainactivity;private Hashmap<integer, integer> map;public soundplayer (MainActivity MainActivity) { this.mainactivity = Mainactivity;this.map = new Hashmap<integer, integer> (),///Soundpool three parameters of the constructor://1. The maximum number of streams played at the same time (it is played at the same time)//2. Stream type//3. Conversion quality This.soundpool = new Soundpool (8, audiomanager.stream_music, 0); This.soundPool.setOnLoadCompleteListener (New Soundpool.onloadcompletelistener () {@Overridepublic void Onloadcomplete (soundpool arg0, int arg1, int arg2) {////////////////To remember that audio is likely to fail to load and must be handled in this case}}); public void Initsounds () {this.map.put (1, This.soundPool.load (this.mainactivity, R.raw.flappy, 1)); This.map.put (2, This.soundPool.load (this.mainactivity, R. Raw.pass, 1)); This.map.put (3, This.soundPool.load (this.mainactivity, R.raw.hit, 1)); This.map.put (4, This.soundPool.load (This.mainactivity, R.raw.die, 1)); This.map.put (5, This.soundPool.load (this.mainactivity, R.raw.swooshing, 1));//Just like this, put your app's voice into the}//where you need to play the audio and the number of times you want to play it in the public void playSound (int sound, int loop {This.soundPool.play (sound, 1, 1, 1, loop, 1.0f);} public void Release () {this.soundPool.release ();}}
Reprint Please specify source: http://blog.csdn.net/gophers
Use Soundpool to play audio in Android