Methods Summary |
---|
private void | acquireWakeLock()
if (mWakeLock != null) {
mWakeLock.acquire();
}
|
private void | enqueueLocked(android.media.AsyncPlayer$Command cmd)
if (mTail == null) {
mHead = cmd;
} else {
mTail.next = cmd;
}
mTail = cmd;
if (mThread == null) {
acquireWakeLock();
mThread = new Thread();
mThread.start();
}
|
public void | play(android.content.Context context, android.net.Uri uri, boolean looping, int stream)Start playing the sound. It will actually start playing at some
point in the future. There are no guarantees about latency here.
Calling this before another audio file is done playing will stop
that one and start the new one.
Command cmd = new Command();
cmd.code = PLAY;
cmd.context = context;
cmd.uri = uri;
cmd.looping = looping;
cmd.stream = stream;
synchronized (mLock) {
enqueueLocked(cmd);
mState = PLAY;
}
|
private void | releaseWakeLock()
if (mWakeLock != null) {
mWakeLock.release();
}
|
public void | setUsesWakeLock(android.content.Context context)We want to hold a wake lock while we do the prepare and play. The stop probably is
optional, but it won't hurt to have it too. The problem is that if you start a sound
while you're holding a wake lock (e.g. an alarm starting a notification), you want the
sound to play, but if the CPU turns off before mThread gets to work, it won't. The
simplest way to deal with this is to make it so there is a wake lock held while the
thread is starting or running. You're going to need the WAKE_LOCK permission if you're
going to call this.
This must be called before the first time play is called.
if (mWakeLock != null || mThread != null) {
// if either of these has happened, we've already played something.
// and our releases will be out of sync.
throw new RuntimeException("assertion failed mWakeLock=" + mWakeLock
+ " mThread=" + mThread);
}
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, mTag);
|
public void | stop()Stop a previously played sound. It can't be played again or unpaused
at this point. Calling this multiple times has no ill effects.
synchronized (mLock) {
// This check allows stop to be called multiple times without starting
// a thread that ends up doing nothing.
if (mState != STOP) {
Command cmd = new Command();
cmd.code = STOP;
enqueueLocked(cmd);
mState = STOP;
}
}
|