FileDocCategorySizeDatePackage
AsyncPlayer.javaAPI DocAndroid 5.1 API8297Thu Mar 12 22:22:30 GMT 2015android.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 static final boolean
mDebug
private final LinkedList
mCmdQueue
private String
mTag
private Thread
mThread
private MediaPlayer
mPlayer
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)

        mCmdQueue.add(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.requestTime = SystemClock.uptimeMillis();
        cmd.code = PLAY;
        cmd.context = context;
        cmd.uri = uri;
        cmd.looping = looping;
        cmd.stream = stream;
        synchronized (mCmdQueue) {
            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);
    
private voidstartSound(android.media.AsyncPlayer$Command cmd)


        
        // Preparing can be slow, so if there is something else
        // is playing, let it continue until we're done, so there
        // is less of a glitch.
        try {
            if (mDebug) Log.d(mTag, "Starting playback");
            MediaPlayer player = new MediaPlayer();
            player.setAudioStreamType(cmd.stream);
            player.setDataSource(cmd.context, cmd.uri);
            player.setLooping(cmd.looping);
            player.prepare();
            player.start();
            if (mPlayer != null) {
                mPlayer.release();
            }
            mPlayer = player;
            long delay = SystemClock.uptimeMillis() - cmd.requestTime;
            if (delay > 1000) {
                Log.w(mTag, "Notification sound delayed by " + delay + "msecs");
            }
        }
        catch (Exception e) {
            Log.w(mTag, "error loading sound for " + cmd.uri, e);
        }
    
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 (mCmdQueue) {
            // 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.requestTime = SystemClock.uptimeMillis();
                cmd.code = STOP;
                enqueueLocked(cmd);
                mState = STOP;
            }
        }