package visizen.com.soundpool;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private SoundPool soundPool;
private int soundId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* Constructor. Constructs a SoundPool object with the following
* characteristics:
*
* @param maxStreams the maximum number of simultaneous streams for this
* SoundPool object
* @param streamType the audio stream type as described in AudioManager
* For example, game applications will normally use
* {@link AudioManager#STREAM_MUSIC}.
* @param srcQuality the sample-rate converter quality. Currently has no
* effect. Use 0 for the default.
* @return a SoundPool object, or null if creation failed
* @deprecated use {@link SoundPool.Builder} instead to create and configure a
* SoundPool instance
*/
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundId = soundPool.load(this, R.raw.ring, 1);
}
public void play(View v) {
/**
* Play a sound from a sound ID.
*
* Play the sound specified by the soundID. This is the value
* returned by the load() function. Returns a non-zero streamID
* if successful, zero if it fails. The streamID can be used to
* further control playback. Note that calling play() may cause
* another sound to stop playing if the maximum number of active
* streams is exceeded. A loop value of -1 means loop forever,
* a value of 0 means don't loop, other values indicate the
* number of repeats, e.g. a value of 1 plays the audio twice.
* The playback rate allows the application to vary the playback
* rate (pitch) of the sound. A value of 1.0 means play back at
* the original frequency. A value of 2.0 means play back twice
* as fast, and a value of 0.5 means playback at half speed.
*
* @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
*/
soundPool.play(soundId, 1.0f, 1.0f, 0, 0, 1.0f);
}
}