public class SoundPlayer
extends java.lang.Object
implements android.media.SoundPool.OnLoadCompleteListener
SoundPlayer
is a simple utility class that plays sounds on the phone. The class
is used through its singleton instance.Modifier and Type | Class and Description |
---|---|
protected static class |
SoundPlayer.CurrentlyPlaying |
protected static class |
SoundPlayer.InstanceHolder |
protected class |
SoundPlayer.LoadedSoundCache
SoundPlayer.LoadedSoundCache keeps track of loaded sounds, mapping sound resource id to loaded
sound id. |
static class |
SoundPlayer.PlaySoundParams |
protected static interface |
SoundPlayer.SoundFromFile |
protected class |
SoundPlayer.SoundInfo |
protected static class |
SoundPlayer.StopWhat |
Modifier and Type | Field and Description |
---|---|
protected SoundPlayer.SoundInfo |
currentlyLoadingInfo |
protected java.util.concurrent.CountDownLatch |
currentlyLoadingLatch |
protected java.util.Set<SoundPlayer.CurrentlyPlaying> |
currentlyPlayingSounds |
protected boolean |
isRobotController |
protected SoundPlayer.LoadedSoundCache |
loadedSounds |
protected java.lang.Object |
lock |
protected float |
masterVolume |
protected android.media.MediaPlayer |
mediaSizer |
static int |
msSoundTransmissionFreshness |
protected java.util.concurrent.ScheduledExecutorService |
scheduledThreadPool |
protected android.content.SharedPreferences |
sharedPreferences |
protected float |
soundOffVolume |
protected float |
soundOnVolume |
protected android.media.SoundPool |
soundPool |
static java.lang.String |
TAG |
protected java.util.concurrent.ExecutorService |
threadPool |
static boolean |
TRACE |
protected Tracer |
tracer |
Constructor and Description |
---|
SoundPlayer(int simultaneousStreams,
int cacheSize)
Instantiates a new sound player.
|
Modifier and Type | Method and Description |
---|---|
static SoundPlayer.SoundInfo |
addRef(SoundPlayer.SoundInfo soundInfo) |
void |
close() |
protected static void |
copy(java.io.InputStream inputStream,
java.io.OutputStream outputStream,
int cbToCopy) |
protected void |
ensureCached(android.content.Context context,
int resId)
Ensures this local sound is also in the local cache.
|
protected SoundPlayer.SoundInfo |
ensureCached(java.lang.String hashString,
SoundPlayer.SoundFromFile ifAbsent)
returns a new ref on the returned
SoundPlayer.SoundInfo ; caller must releaseRef() |
protected SoundPlayer.SoundInfo |
ensureLoaded(android.content.Context context,
java.io.File file) |
protected SoundPlayer.SoundInfo |
ensureLoaded(android.content.Context context,
int resourceId) |
protected SoundPlayer.SoundInfo |
ensureLoaded(java.lang.String hashString,
SoundPlayer.SoundFromFile ifAbsent)
returns a new ref on the returned
SoundPlayer.SoundInfo ; caller must releaseRef() |
static SoundPlayer |
getInstance() |
float |
getMasterVolume()
Returns the master volume control that is applied to all played sounds
|
protected int |
getMsDuration(android.content.Context context,
java.io.File file) |
protected int |
getMsDuration(android.content.Context context,
int resourceId) |
protected long |
getMsNow() |
CallbackResult |
handleCommandPlaySound(java.lang.String extra) |
CallbackResult |
handleCommandRequestSound(Command requestSoundCommand) |
CallbackResult |
handleCommandStopPlayingSounds(Command stopPlayingSoundsCommand) |
protected void |
internalStopPlaying(SoundPlayer.StopWhat stopWhat) |
boolean |
isLocalSoundOn() |
protected void |
loadAndStartPlaying(android.content.Context context,
java.io.File file,
SoundPlayer.PlaySoundParams params,
<any> runWhenStarted,
java.lang.Runnable runWhenFinished) |
protected void |
loadAndStartPlaying(android.content.Context context,
int resourceId,
SoundPlayer.PlaySoundParams params,
<any> runWhenStarted,
java.lang.Runnable runWhenFinished) |
void |
onLoadComplete(android.media.SoundPool soundPool,
int sampleId,
int status) |
void |
play(android.content.Context context,
java.io.File file,
float volume,
int loop,
float rate) |
void |
play(android.content.Context context,
int resId)
Deprecated.
use
startPlaying(Context, int) instead |
void |
play(android.content.Context context,
int resId,
boolean waitForCompletion)
Deprecated.
use
#startPlaying(Context, int, PlaySoundParams, Consumer, Runnable) instead |
void |
prefillSoundCache(int... resourceIds)
Ensures that these local sounds are also in the local cache
|
boolean |
preload(android.content.Context context,
java.io.File file)
Preloads the sound so as to to reduce delays if the sound is subsequently played.
|
boolean |
preload(android.content.Context context,
int resourceId)
Preloads the sound so as to to reduce delays if the sound is subsequently played.
|
static void |
releaseRef(SoundPlayer.SoundInfo soundInfo) |
protected void |
safeClose(java.lang.Object closeable) |
void |
setMasterVolume(float masterVolume)
Sets the master volume control that is applied to all played sounds
|
void |
startPlaying(android.content.Context context,
java.io.File file) |
void |
startPlaying(android.content.Context context,
java.io.File file,
SoundPlayer.PlaySoundParams params,
<any> runWhenStarted,
java.lang.Runnable runWhenFinished) |
void |
startPlaying(android.content.Context context,
int resId)
Asynchronously loads the indicated sound from its resource (if not already loaded), then
initiates its play once any current non-looping sound is finished playing.
|
void |
startPlaying(android.content.Context context,
int resId,
SoundPlayer.PlaySoundParams params,
<any> runWhenStarted,
java.lang.Runnable runWhenFinished)
Asynchronously loads the indicated sound from its resource (if not already loaded), then
initiates its play, optionally waiting for any currently non-looping playing sounds to finish first.
|
protected void |
startPlayingLoadedSound(SoundPlayer.SoundInfo soundInfo,
SoundPlayer.PlaySoundParams paramsIn,
<any> runWhenStarted,
java.lang.Runnable runWhenFinished) |
void |
stopPlayingAll()
Stops playing all sounds that are currently playing
|
void |
stopPlayingLoops()
Stops playing all sounds that are currently playing in a loop
|
protected void |
waitForLoadCompletion() |
public static final java.lang.String TAG
public static boolean TRACE
protected Tracer tracer
public static final int msSoundTransmissionFreshness
protected final java.lang.Object lock
protected final boolean isRobotController
protected android.media.SoundPool soundPool
protected java.util.concurrent.CountDownLatch currentlyLoadingLatch
protected SoundPlayer.SoundInfo currentlyLoadingInfo
protected SoundPlayer.LoadedSoundCache loadedSounds
protected java.util.concurrent.ExecutorService threadPool
protected java.util.concurrent.ScheduledExecutorService scheduledThreadPool
protected android.content.SharedPreferences sharedPreferences
protected float soundOnVolume
protected float soundOffVolume
protected float masterVolume
protected android.media.MediaPlayer mediaSizer
protected java.util.Set<SoundPlayer.CurrentlyPlaying> currentlyPlayingSounds
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()
public void prefillSoundCache(int... resourceIds)
public void startPlaying(android.content.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 startPlaying(android.content.Context context, java.io.File file)
public void startPlaying(android.content.Context context, int resId, SoundPlayer.PlaySoundParams params, <any> runWhenStarted, java.lang.Runnable runWhenFinished)
context
- the context in which resId is to be interpretedresId
- the resource id of the raw resource containing the sound.params
- controls how the playback proceedsrunWhenStarted
- executed when the stream starts to playrunWhenFinished
- executed when the stream finishes playingpublic void startPlaying(android.content.Context context, java.io.File file, SoundPlayer.PlaySoundParams params, <any> runWhenStarted, java.lang.Runnable runWhenFinished)
public void stopPlayingAll()
public void stopPlayingLoops()
protected void internalStopPlaying(SoundPlayer.StopWhat stopWhat)
public boolean preload(android.content.Context context, int resourceId)
public boolean preload(android.content.Context context, java.io.File file)
public void setMasterVolume(float masterVolume)
getMasterVolume()
public float getMasterVolume()
setMasterVolume(float)
@Deprecated public void play(android.content.Context context, int resId)
startPlaying(Context, int)
instead@Deprecated public void play(android.content.Context context, int resId, boolean waitForCompletion)
#startPlaying(Context, int, PlaySoundParams, Consumer, Runnable)
insteadprotected void loadAndStartPlaying(android.content.Context context, int resourceId, SoundPlayer.PlaySoundParams params, <any> runWhenStarted, java.lang.Runnable runWhenFinished)
protected void loadAndStartPlaying(android.content.Context context, java.io.File file, SoundPlayer.PlaySoundParams params, <any> runWhenStarted, java.lang.Runnable runWhenFinished)
protected SoundPlayer.SoundInfo ensureLoaded(android.content.Context context, int resourceId)
protected SoundPlayer.SoundInfo ensureLoaded(android.content.Context context, java.io.File file)
public boolean isLocalSoundOn()
protected void startPlayingLoadedSound(SoundPlayer.SoundInfo soundInfo, SoundPlayer.PlaySoundParams paramsIn, <any> runWhenStarted, java.lang.Runnable runWhenFinished)
protected int getMsDuration(android.content.Context context, int resourceId)
protected int getMsDuration(android.content.Context context, java.io.File file)
protected void waitForLoadCompletion()
public void onLoadComplete(android.media.SoundPool soundPool, int sampleId, int status)
onLoadComplete
in interface android.media.SoundPool.OnLoadCompleteListener
protected long getMsNow()
protected SoundPlayer.SoundInfo ensureLoaded(java.lang.String hashString, SoundPlayer.SoundFromFile ifAbsent)
SoundPlayer.SoundInfo
; caller must releaseRef()protected void ensureCached(android.content.Context context, int resId)
protected SoundPlayer.SoundInfo ensureCached(java.lang.String hashString, SoundPlayer.SoundFromFile ifAbsent)
SoundPlayer.SoundInfo
; caller must releaseRef()public CallbackResult handleCommandPlaySound(java.lang.String extra)
public CallbackResult handleCommandStopPlayingSounds(Command stopPlayingSoundsCommand)
protected static void copy(java.io.InputStream inputStream, java.io.OutputStream outputStream, int cbToCopy) throws java.io.IOException
java.io.IOException
public CallbackResult handleCommandRequestSound(Command requestSoundCommand)
protected void safeClose(java.lang.Object closeable)
public static SoundPlayer.SoundInfo addRef(SoundPlayer.SoundInfo soundInfo)
public static void releaseRef(SoundPlayer.SoundInfo soundInfo)
public void play(android.content.Context context, java.io.File file, float volume, int loop, float rate)