public class AudioPlayer
extends java.lang.Object
implements android.media.MediaPlayer.OnPreparedListener, android.media.MediaPlayer.OnCompletionListener, android.media.MediaPlayer.OnBufferingUpdateListener
Modifier and Type | Class and Description |
---|---|
static class |
AudioPlayer.AppState
States enum.
|
static class |
AudioPlayer.AppTrigger
Triggers enum.
|
static class |
AudioPlayer.AudioType
An enum listing types' refinements of audio states.
|
(package private) class |
AudioPlayer.BufferEntry
Action taken before the machine enters
AudioPlayer.AppState.BUFFERING -> watch for updates. |
(package private) class |
AudioPlayer.CompleteEntry |
(package private) class |
AudioPlayer.PauseEntry
Action taken when the state machine enters
AudioPlayer.AppState.PAUSED -> pauses the player_ . |
(package private) class |
AudioPlayer.PlayEntry
Action taken before the state machine enters
AudioPlayer.AppState.PLAYING , one we have a prepared
player_ -> starts buffering and sets this audio_type_ to take focus over
the shared SharedBuffer.audio_type() . |
(package private) class |
AudioPlayer.PlayGuard
Guard that prevents the machine to enters
AudioPlayer.AppState.PLAYING is the player_ isn't prepared. |
(package private) class |
AudioPlayer.PrepareEntry
Action taken when the state machine enters
AudioPlayer.AppState.PREPARING -> select_track()
and prepare the player_ . |
Modifier and Type | Field and Description |
---|---|
private AudioPlayer.AudioType |
audio_type_
The
AudioPlayer.AudioType handled by this player. |
(package private) android.os.Handler |
buffer_handler_
Thread tracked audio stream buffering.
|
private float |
buffer_progress_
The fraction of buffered audio time.
|
(package private) java.lang.Runnable |
buffer_runnable_
Buffering thread action's interface.
|
(package private) android.content.Context |
context_
MainActivity 's Context backref. |
private ValueAnimator.Builder |
fader_ |
(package private) com.github.oxo42.stateless4j.StateMachine<AudioPlayer.AppState,AudioPlayer.AppTrigger> |
fsm_
The state machine.
|
(package private) com.github.oxo42.stateless4j.StateMachineConfig<AudioPlayer.AppState,AudioPlayer.AppTrigger> |
fsm_config_
The state machine config.
|
(package private) android.media.MediaPlayer |
player_
The player.
|
(package private) boolean |
prepared_
Indicates that a sound has been prepared and is ready to play.
|
private android.net.Uri |
track_
An
Uri pointing to an audio file. |
Constructor and Description |
---|
AudioPlayer(android.content.Context context,
AudioPlayer.AudioType audio_type)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
private void |
buffer_() |
private int |
buffered_ms() |
(package private) void |
clear_(AudioPlayer.AppState target_state)
Shortcut to remove listeners and clears the player according some
AudioPlayer.AppState parameter. |
void |
destroy() |
private void |
fade_in(long duration) |
(package private) void |
fade_out(AudioPlayer.AppState target_state,
long duration)
Fades audio out.
|
void |
onBufferingUpdate(android.media.MediaPlayer media_player,
int percent)
MediaPlayer.onBufferingUpdate implementation -> updates buffer_progress_ |
void |
onCompletion(android.media.MediaPlayer media_player)
MediaPlayer.OnCompletionListener implementation -> SharedBuffer#set_audio_state(AppState)
to AudioPlayer.AppState.COMPLETED and notify SharedBuffer.dispatcher() . |
void |
onPrepared(android.media.MediaPlayer media_player)
MediaPlayer.OnPreparedListener implementation -> triggers play() . |
void |
pause()
pause() trigger -> will either:
drives the machine to AudioPlayer.AppState.IDLE if the sound is not playing;
drives the machine to AudioPlayer.AppState.PAUSED otherwise.
|
(package private) void |
play() |
private android.media.MediaPlayer |
player() |
(package private) android.net.Uri |
select_track()
Returns a new track, should be overridden in children classes.
|
(package private) void |
share(AudioPlayer.AppState state,
java.lang.String event,
boolean dispatch) |
AudioPlayer.AppState |
state()
Returns the current state of the
fsm_ . |
@NonNull final android.os.Handler buffer_handler_
@NonNull final java.lang.Runnable buffer_runnable_
final android.content.Context context_
MainActivity
's Context
backref.final com.github.oxo42.stateless4j.StateMachineConfig<AudioPlayer.AppState,AudioPlayer.AppTrigger> fsm_config_
@NonNull private final AudioPlayer.AudioType audio_type_
AudioPlayer.AudioType
handled by this player.@NonNull private final ValueAnimator.Builder fader_
@Nullable private android.net.Uri track_
Uri
pointing to an audio file.@Nullable android.media.MediaPlayer player_
boolean prepared_
com.github.oxo42.stateless4j.StateMachine<AudioPlayer.AppState,AudioPlayer.AppTrigger> fsm_
private float buffer_progress_
AudioPlayer(@NonNull android.content.Context context, @NonNull AudioPlayer.AudioType audio_type)
context
- A context
's activity backrefaudio_type
- The AudioPlayer.AudioType
this player behaves aroundprivate int buffered_ms()
void play()
private void buffer_()
void fade_out(AudioPlayer.AppState target_state, long duration)
share(AppState, String, boolean)
but only clear_(AppState)
the player once volume has faded away.target_state
- The target AudioPlayer.AppState
duration
- Duration of the effectvoid share(@NonNull AudioPlayer.AppState state, @Nullable java.lang.String event, boolean dispatch)
void clear_(AudioPlayer.AppState target_state)
AudioPlayer.AppState
parameter.target_state
- The state on which the SharedBuffer.audio_state_
should be set.private void fade_in(long duration)
public void onBufferingUpdate(android.media.MediaPlayer media_player, int percent)
MediaPlayer.onBufferingUpdate
implementation -> updates buffer_progress_
onBufferingUpdate
in interface android.media.MediaPlayer.OnBufferingUpdateListener
media_player
- The MediaPlayer
receiving updatespercent
- The amount of buffered audio in percentpublic void onCompletion(android.media.MediaPlayer media_player)
MediaPlayer.OnCompletionListener
implementation -> SharedBuffer#set_audio_state(AppState)
to AudioPlayer.AppState.COMPLETED
and notify SharedBuffer.dispatcher()
.onCompletion
in interface android.media.MediaPlayer.OnCompletionListener
media_player
- public void onPrepared(android.media.MediaPlayer media_player)
MediaPlayer.OnPreparedListener
implementation -> triggers play()
.onPrepared
in interface android.media.MediaPlayer.OnPreparedListener
media_player
- @Nullable android.net.Uri select_track()
Uri
track.public AudioPlayer.AppState state()
fsm_
.AudioPlayer.AppState
variable.public void pause()
pause()
trigger -> will either:
AudioPlayer.AppState.IDLE
if the sound is not playing;AudioPlayer.AppState.PAUSED
otherwise.public void destroy()
private android.media.MediaPlayer player()