Build 1.0_r1(from source)

android.media
Class AsyncPlayer

java.lang.Object
  extended by android.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.


Constructor Summary
AsyncPlayer(String tag)
          Construct an AsyncPlayer object.
 
Method Summary
 void play(Context context, Uri uri, boolean looping, int stream)
          Start playing the sound.
 void setUsesWakeLock(Context context)
          We want to hold a wake lock while we do the prepare and play.
 void stop()
          Stop a previously played sound.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AsyncPlayer

public AsyncPlayer(String tag)
Construct an AsyncPlayer object.

Parameters:
tag - a string to use for debugging
Method Detail

play

public void play(Context context,
                 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.

Parameters:
context - Your application's context.
uri - The URI to play. (see MediaPlayer.setDataSource(Context, Uri))
looping - Whether the audio should loop forever. (see MediaPlayer.setLooping(boolean))
stream - the AudioStream to use. (see MediaPlayer.setAudioStreamType(int))

stop

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.


setUsesWakeLock

public void setUsesWakeLock(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.


Build 1.0_r1(from source)

Please submit a feedback, bug or feature