public class NotificationPlayer extends Object implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener
Constructor and Description |
---|
NotificationPlayer(String tag)
Construct a NotificationPlayer object.
|
Modifier and Type | Method and Description |
---|---|
void |
onCompletion(MediaPlayer mp)
Called when the end of a media source is reached during playback.
|
boolean |
onError(MediaPlayer mp,
int what,
int extra)
Called to indicate an error.
|
void |
play(Context context,
Uri uri,
boolean looping,
AudioAttributes attributes)
Start playing the sound.
|
void |
play(Context context,
Uri uri,
boolean looping,
int stream)
Deprecated.
use
play(Context, Uri, boolean, AudioAttributes) instead. |
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.
|
public NotificationPlayer(String tag)
tag
- a string to use for debuggingpublic void onCompletion(MediaPlayer mp)
MediaPlayer.OnCompletionListener
onCompletion
in interface MediaPlayer.OnCompletionListener
mp
- the MediaPlayer that reached the end of the filepublic boolean onError(MediaPlayer mp, int what, int extra)
MediaPlayer.OnErrorListener
onError
in interface MediaPlayer.OnErrorListener
mp
- the MediaPlayer the error pertains towhat
- the type of error that has occurred:
extra
- an extra code, specific to the error. Typically
implementation dependent.
MediaPlayer.MEDIA_ERROR_IO
MediaPlayer.MEDIA_ERROR_MALFORMED
MediaPlayer.MEDIA_ERROR_UNSUPPORTED
MediaPlayer.MEDIA_ERROR_TIMED_OUT
MEDIA_ERROR_SYSTEM (-2147483648)
- low-level system error.
@Deprecated public void play(Context context, Uri uri, boolean looping, int stream)
play(Context, Uri, boolean, AudioAttributes)
instead.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)
)public void play(Context context, Uri uri, boolean looping, AudioAttributes attributes)
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)
)attributes
- the AudioAttributes to use.
(see MediaPlayer.setAudioAttributes(AudioAttributes)
)public void stop()
public void setUsesWakeLock(Context context)