FileDocCategorySizeDatePackage
AsyncPlayer.javaAPI DocAndroid 1.5 API8078Wed May 06 22:42:00 BST 2009android.media

AsyncPlayer

public class AsyncPlayer extends Object
Plays a series of audio URIs, but does all the hard work on another thread so that any slowness with preparing or loading doesn't block the calling thread.

Fields Summary
private static final int
PLAY
private static final int
STOP
private String
mTag
private Command
mHead
private Command
mTail
private Thread
mThread
private MediaPlayer
mPlayer
private Object
mLock
private PowerManager.WakeLock
mWakeLock
private int
mState
Constructors Summary
public AsyncPlayer(String tag)
Construct an AsyncPlayer object.

param
tag a string to use for debugging


                     
       
        if (tag != null) {
            mTag = tag;
        } else {
            mTag = "AsyncPlayer";
        }
    
Methods Summary
private voidacquireWakeLock()

        if (mWakeLock != null) {
            mWakeLock.acquire();
        }
    
private voidenqueueLocked(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 voidplay(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.

param
context Your application's context.
param
uri The URI to play. (see {@link MediaPlayer#setDataSource(Context, Uri)})
param
looping Whether the audio should loop forever. (see {@link MediaPlayer#setLooping(boolean)})
param
stream the AudioStream to use. (see {@link MediaPlayer#setAudioStreamType(int)})

        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 voidreleaseWakeLock()

        if (mWakeLock != null) {
            mWakeLock.release();
        }
    
public voidsetUsesWakeLock(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.

hide

        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 voidstop()
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;
            }
        }