Use Soundpool to play audio in Android

Source: Internet
Author: User

Looking for material to re-do Flappybird today, I learned how to set the background audio for an app, and found that by encapsulating the Soundpool class, you can do it well.


The Soundpool class is better suited to play some of the more ephemeral and smaller audio streams like game sounds, and it supports simultaneous playback of multiple audio streams, while larger audio is more suitable for MediaPlayer.

Give a general explanation of the basic usage of the Soundpool class when using:

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 was 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, loop is the number of plays (-1 is the infinite loop), rate is the playback speed ~


The following is a class of playback audio encapsulated in the redo Flappybird, which is more efficient when compared with MediaPlayer, which plays the sound effect:

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 The constructor of the three parameters respectively://1. The maximum number of simultaneous streams played (OH)//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 accordingly}}); 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, load all the sounds your app needs to get into the}//where you need to play the audio and pass in 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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.