public class SoundPlayer
extends java.lang.Object
SoundPlayer
is a simple utility class that plays sounds on the phone. The class
is typically used through its singleton instance.play(Context, int)
Modifier and Type | Class and Description |
---|---|
protected class |
SoundPlayer.LoadedSoundCache
SoundPlayer.LoadedSoundCache keeps track of loaded sounds, mapping sound resource id to loaded
sound id. |
protected class |
SoundPlayer.SoundInfo |
Modifier and Type | Field and Description |
---|---|
protected Context |
context |
protected int |
currentlyLoading |
static boolean |
DEBUG |
protected java.util.concurrent.ExecutorService |
executorService |
protected SoundPlayer.LoadedSoundCache |
loadedSounds |
protected Looper |
looper |
protected long |
msFinishPlaying |
protected SharedPreferences |
sharedPreferences |
protected float |
soundOffLevel |
protected float |
soundOnLevel |
protected SoundPool |
soundPool |
static java.lang.String |
TAG |
protected static SoundPlayer |
theInstance |
Constructor and Description |
---|
SoundPlayer(int simultaneousStreams,
int cacheSize)
Instantiates a new sound player.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
protected long |
getCurrentMilliseconds() |
static SoundPlayer |
getInstance() |
protected int |
getMsDuration(Context context,
int resourceId) |
protected void |
loadAndPlay(Context context,
int resourceId,
boolean waitForCompletion)
Loads the requested sound if necessary, then (eventually) plays it.
|
void |
onLoadComplete(SoundPool soundPool,
int sampleId,
int status)
Called when a sound has completed loading.
|
void |
play(Context context,
int resId)
Asynchronously loads the indicated sound from its resource (if not already loaded), then
initiates its play once any current sound is finished playing.
|
void |
play(Context context,
int resId,
boolean waitForCompletion)
Asynchronously loads the indicated sound from its resource (if not already loaded), then
initiates its play, optionally waiting for any currently playing sound to finish first.
|
protected void |
playLoadedSound(SoundPlayer.SoundInfo soundInfo,
boolean waitForCompletion) |
protected void |
shutdown() |
protected void |
startup() |
protected void |
waitForLoadCompletion() |
public static final java.lang.String TAG
public static final boolean DEBUG
protected static SoundPlayer theInstance
protected SoundPool soundPool
protected volatile int currentlyLoading
protected long msFinishPlaying
protected SoundPlayer.LoadedSoundCache loadedSounds
protected java.util.concurrent.ExecutorService executorService
protected Looper looper
protected Context context
protected SharedPreferences sharedPreferences
protected float soundOnLevel
protected float soundOffLevel
public SoundPlayer(int simultaneousStreams, int cacheSize)
simultaneousStreams
- the number of sounds that can simultaneously play from this player.
If one, then playing any new sound interrupts the playing of a
a previous soundcacheSize
- the maximum size of the cache of loaded sounds.public static SoundPlayer getInstance()
public void close()
protected void startup()
protected void shutdown()
public void play(Context context, int resId)
context
- the context in which resId is to be interpretedresId
- the resource id of the raw resource containing the sound.public void play(Context context, int resId, boolean waitForCompletion)
context
- the context in which resId is to be interpretedresId
- the resource id of the raw resource containing the sound.waitForCompletion
- whether to wait for any current sound to finish playing first or notprotected void loadAndPlay(Context context, int resourceId, boolean waitForCompletion)
protected int getMsDuration(Context context, int resourceId)
protected void waitForLoadCompletion()
protected void playLoadedSound(SoundPlayer.SoundInfo soundInfo, boolean waitForCompletion)
protected long getCurrentMilliseconds()
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
soundPool
- SoundPool object from the load() methodsampleId
- the sample ID of the sound loaded.status
- the status of the load operation (0 = success)