sk.baka.ambient
Class PlaylistPlayer

java.lang.Object
  extended by sk.baka.ambient.PlaylistPlayer
All Implemented Interfaces:
AppState.IAppStateAware, IPlaylistStrategy

public final class PlaylistPlayer
extends Object
implements IPlaylistStrategy, AppState.IAppStateAware

This is THE component :) Combines player and playlist. Emits IPlaylistPlayerListener messages.

Author:
Martin Vysny

Field Summary
(package private)  IPlayer playerService
          The player service invocator.
 
Constructor Summary
PlaylistPlayer(AmbientApplication app)
          Creates new player instance.
 
Method Summary
 void add(int index, List<TrackMetadataBean> tracks)
          Inserts given tracks before track with given index.
 void add(int index, TrackMetadataBean track)
          Adds track to the playlist after given track
 void add(TrackMetadataBean track)
          Adds track to the end of the playlist.
 void clearQueue()
          Clears queue from queued.
 void close()
          Closes and deactivates the player.
 void dequeue(int track)
          Dequeues given track if it was queued previously.
 void dynamicPlaylist()
          Changes the playlist backend to be a dynamic playlist.
 int getCurrentlyPlaying()
          Returns currently playing track.
 PlaylistItem getCurrentlyPlayingItem()
          Returns currently playing track.
 TrackMetadataBean getCurrentlyPlayingTrack()
          Returns currently playing track.
 PlayerStateEnum getPlaybackState()
          Returns the playback state.
 List<PlaylistItem> getPlayItems()
           Returns list of playlist items.
 int getPosition()
          Returns current position in millis of the playback.
 List<Integer> getQueue()
          Returns currently queued tracks.
 Random getRandom()
          Returns current random mode.
 Repeat getRepeat()
          Returns the repeat mode.
 boolean isDynamic()
          Returns true if the playlist is a dynamic playlist.
 Interval move(Interval interval, int targetIndex)
          Moves selected tracks before track with given index.
 Interval moveBy(Interval interval, int delta)
           Moves selected tracks up or down at least delta tracks, depending on the 'delta' value.
 int next()
          Moves to next track to be played.
 void pause()
          Pauses the player, or resumes playback if the player was paused.
 int peekNext()
          Peeks at the next track without actually changing the current track.
 void play()
          Plays current track from the beginning.
 int play(int trackToPlay)
          Plays given track.
 void playWithSeek(int startSeekMillis)
          Plays current track from the beginning.
 int playWithSeek(int trackToPlay, int startSeekMillis)
          Plays given track.
 int previous()
          Returns previous track to be played.
(package private)  void psPlay(int track, int startSeekMillis)
          Requests the player service to play given track.
 void queue(int track)
          Queues given track, after all other queued tracks.
 void queue(Interval tracks)
          Queue this track for playing, after all other queued tracks.
 void reinit()
          Reinitializes the playlist - recomputes new random track ordering etc.
 void reinit(AppState state)
          Cherry-picks values from the application state and reinitializes itself.
 void reinit(IPlaylistStrategy strategy)
          Reinits the player with given playlist strategy.
 PlaylistItem remove(int index)
          Removes a track with given index from the playlist.
 void remove(Interval interval)
          Removes tracks with given index from the playlist.
 void replaceLocations(Map<String,String> locationMap)
          Modifies the playlist by changing all PlaylistItem locations.
 void seek(int position)
          Seeks to given position.
 void setRandom(Random random)
          Sets the random mode.
 void setRepeat(Repeat repeat)
          Sets the repeat mode.
 void shuffle()
          Randomize the playlist.
 int size()
          Returns the playlist length.
 void sortByAlbumOrder()
          Sorts the playlist with album order ordering.
 void staticPlaylist()
          Changes the playlist backend to be an empty static playlist.
 void stop()
          Stops the playback.
 void storeState(AppState result)
          Stores the components state into the global state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

playerService

IPlayer playerService
The player service invocator.

Constructor Detail

PlaylistPlayer

PlaylistPlayer(AmbientApplication app)
Creates new player instance.

Parameters:
app - the application instance.
Method Detail

stop

public void stop()
Stops the playback. Does nothing if the player is inactive.


getPlaybackState

public PlayerStateEnum getPlaybackState()
Returns the playback state.

Returns:
the playback state.

pause

public void pause()
Pauses the player, or resumes playback if the player was paused. Does nothing if the player is stopped.


seek

public void seek(int position)
Seeks to given position. If the position is invalid then the result is undefined. Does nothing if the playback is stopped.

Parameters:
position - Seek to this position in milliseconds.

add

public void add(int index,
                TrackMetadataBean track)
Adds track to the playlist after given track

Parameters:
index - insert given track after track with this index.
track - the track to add.

add

public void add(int index,
                List<TrackMetadataBean> tracks)
Description copied from interface: IPlaylistStrategy
Inserts given tracks before track with given index.

Specified by:
add in interface IPlaylistStrategy
Parameters:
index - the index, must not be negative.
tracks - the tracks meta, must not be null.

add

public void add(TrackMetadataBean track)
Adds track to the end of the playlist.

Parameters:
track - the track to add.

clearQueue

public void clearQueue()
Description copied from interface: IPlaylistStrategy
Clears queue from queued.

Specified by:
clearQueue in interface IPlaylistStrategy

dequeue

public void dequeue(int track)
Description copied from interface: IPlaylistStrategy
Dequeues given track if it was queued previously. Does nothing if the track is not present in the queue anymore.

Specified by:
dequeue in interface IPlaylistStrategy
Parameters:
track - the track to dequeue.

getCurrentlyPlaying

public int getCurrentlyPlaying()
Description copied from interface: IPlaylistStrategy
Returns currently playing track. Note that the playlist is not connected to the player and the player itself may be paused or stopped.

Specified by:
getCurrentlyPlaying in interface IPlaylistStrategy
Returns:
currently playing track or -1 if nothing is being played. Index to the IPlaylistStrategy.getPlayItems() list.

getCurrentlyPlayingTrack

public TrackMetadataBean getCurrentlyPlayingTrack()
Returns currently playing track.

Returns:
current track or null if no track is being played ( getCurrentlyPlaying() returns -1).

getCurrentlyPlayingItem

public PlaylistItem getCurrentlyPlayingItem()
Returns currently playing track.

Returns:
current track or null if no track is being played ( getCurrentlyPlaying() returns -1).

getPlayItems

public List<PlaylistItem> getPlayItems()
Description copied from interface: IPlaylistStrategy

Returns list of playlist items. The operation must be quick - it should for example provide immutable view on an internal playlist structure, it should not recompute the list anew on each call.

Each PlaylistItem in the list must be an unique instance.

Specified by:
getPlayItems in interface IPlaylistStrategy
Returns:
list of playlist items, will not be modified.

next

public int next()
Description copied from interface: IPlaylistStrategy
Moves to next track to be played. Returns -1 if no more tracks are to be played - this also causes the IPlaylistStrategy.getCurrentlyPlaying() method to return -1. Calling this method while not playing anything will start to play first track.

Specified by:
next in interface IPlaylistStrategy
Returns:
play item to play next or -1 if no more items are to be played. Index to the IPlaylistStrategy.getPlayItems() list.

play

public int play(int trackToPlay)
Description copied from interface: IPlaylistStrategy
Plays given track. The playback continues from this track forward, unless random play is activated.

Specified by:
play in interface IPlaylistStrategy
Parameters:
trackToPlay - the track to play. Index to the IPlaylistStrategy.getPlayItems() list. If -1 then the current track pointer is moved before first track and the playback is stopped.
Returns:
the track that is being played.

playWithSeek

public int playWithSeek(int trackToPlay,
                        int startSeekMillis)
Plays given track. The playback continues from this track forward, unless random play is activated.

Parameters:
trackToPlay - the track to play. Index to the getPlayItems() list. If -1 then the current track pointer is moved before first track and the playback is stopped.
startSeekMillis - start playback here. Ignored when the stream is not seekable.
Returns:
the track that is being played.

play

public void play()
Plays current track from the beginning.


playWithSeek

public void playWithSeek(int startSeekMillis)
Plays current track from the beginning.

Parameters:
startSeekMillis - start playback here. Ignored when the stream is not seekable.

previous

public int previous()
Description copied from interface: IPlaylistStrategy
Returns previous track to be played. Returns -1 if we are at the beginning of the playing track sequence. Calling this method while not playing anything will start to play last track.

Specified by:
previous in interface IPlaylistStrategy
Returns:
which item was played prior current track or -1 if we are at the beginning of the playing track sequence.

psPlay

void psPlay(int track,
            int startSeekMillis)
Requests the player service to play given track. The playlist is not updated.

Parameters:
track - the track to play.
startSeekMillis - start seek time, in milliseconds.

queue

public void queue(int track)
Queues given track, after all other queued tracks. Does nothing if the track is already queued.

Parameters:
track - the track to queue, index to the getPlayItems() list.

queue

public void queue(Interval tracks)
Description copied from interface: IPlaylistStrategy
Queue this track for playing, after all other queued tracks. Does nothing if the track is already queued.

Specified by:
queue in interface IPlaylistStrategy
Parameters:
tracks - the tracks to queue, index to the IPlaylistStrategy.getPlayItems() list.

reinit

public void reinit()
Description copied from interface: IPlaylistStrategy
Reinitializes the playlist - recomputes new random track ordering etc. Removes all queued tracks and stops the playback (IPlaylistStrategy.getCurrentlyPlaying() will return -1).

Specified by:
reinit in interface IPlaylistStrategy

remove

public PlaylistItem remove(int index)
Removes a track with given index from the playlist.

Parameters:
index - the index.
Returns:
the removed track. If currently played track is removed then the playlist should move to play a next song.

isDynamic

public boolean isDynamic()
Returns true if the playlist is a dynamic playlist.

Returns:
true if current playlist is dynamic.

remove

public void remove(Interval interval)
Description copied from interface: IPlaylistStrategy
Removes tracks with given index from the playlist. If currently played track is removed then the current song must be set to -1.

Specified by:
remove in interface IPlaylistStrategy
Parameters:
interval - the interval to remove

setRandom

public void setRandom(Random random)
Description copied from interface: IPlaylistStrategy
Sets the random mode. Currently played track must be preserved.

Specified by:
setRandom in interface IPlaylistStrategy
Parameters:
random - the random mode, never null.

getRandom

public Random getRandom()
Description copied from interface: IPlaylistStrategy
Returns current random mode.

Specified by:
getRandom in interface IPlaylistStrategy
Returns:
the random mode, never null.

shuffle

public void shuffle()
Description copied from interface: IPlaylistStrategy
Randomize the playlist.

Specified by:
shuffle in interface IPlaylistStrategy

size

public int size()
Description copied from interface: IPlaylistStrategy
Returns the playlist length.

Specified by:
size in interface IPlaylistStrategy
Returns:
the playlist length.

sortByAlbumOrder

public void sortByAlbumOrder()
Description copied from interface: IPlaylistStrategy
Sorts the playlist with album order ordering.

Specified by:
sortByAlbumOrder in interface IPlaylistStrategy

dynamicPlaylist

public void dynamicPlaylist()
Changes the playlist backend to be a dynamic playlist. The playlist is populated automatically.


staticPlaylist

public void staticPlaylist()
Changes the playlist backend to be an empty static playlist.


close

public void close()
Closes and deactivates the player. The instance is no more usable.


setRepeat

public void setRepeat(Repeat repeat)
Sets the repeat mode.

Parameters:
repeat - repeat mode, must not be null.

getRepeat

public Repeat getRepeat()
Returns the repeat mode.

Returns:
repeat mode, must not be null.

getPosition

public int getPosition()
Returns current position in millis of the playback. Returns 0 if the playback is stopped.

Returns:
current player position in millis.

getQueue

public List<Integer> getQueue()
Description copied from interface: IPlaylistStrategy
Returns currently queued tracks.

Specified by:
getQueue in interface IPlaylistStrategy
Returns:
queued tracks, never null.

move

public Interval move(Interval interval,
                     int targetIndex)
Description copied from interface: IPlaylistStrategy
Moves selected tracks before track with given index. Correctly updates queue indices. Currently played track will not be changed, although the track index may change.

Specified by:
move in interface IPlaylistStrategy
Parameters:
interval - the interval to move
targetIndex - move tracks before track with this index. If this index is contained in the interval then nothing is done.
Returns:
a new interval which contains all moved tracks.

reinit

public void reinit(IPlaylistStrategy strategy)
Reinits the player with given playlist strategy.

Parameters:
strategy - the deserialized strategy

reinit

public void reinit(AppState state)
Description copied from interface: AppState.IAppStateAware
Cherry-picks values from the application state and reinitializes itself.

Specified by:
reinit in interface AppState.IAppStateAware
Parameters:
state - the just-loaded state

storeState

public void storeState(AppState result)
Description copied from interface: AppState.IAppStateAware
Stores the components state into the global state. Must not alter settings not applicable for this component.

Specified by:
storeState in interface AppState.IAppStateAware
Parameters:
result - the object to store to.

moveBy

public Interval moveBy(Interval interval,
                       int delta)
Description copied from interface: IPlaylistStrategy

Moves selected tracks up or down at least delta tracks, depending on the 'delta' value. The playlist may decide to move tracks by more tracks than requested - for example, the dynamic playlist will skip the currently playing/queued tracks. If the move is not possible (for example there are not enough tracks) then move the tracks to the beginning (or end) of the playlist.

Currently played track will not be changed, although the queue indices. Currently played track will not be changed, although the track index may change.

Specified by:
moveBy in interface IPlaylistStrategy
Parameters:
interval - the interval to move
delta - move tracks up 'delta' tracks (if delta is negative), or down 'delta' tracks (if delta is positive). Do nothing if delta is zero.
Returns:
a new interval which contains all moved tracks.

peekNext

public int peekNext()
Description copied from interface: IPlaylistStrategy
Peeks at the next track without actually changing the current track.

Specified by:
peekNext in interface IPlaylistStrategy
Returns:
next track index. Returns -1 when there's no track left. Dynamic playlist may return -1 when there are no upcoming tracks and the queue is empty.

replaceLocations

public void replaceLocations(Map<String,String> locationMap)
Description copied from interface: IPlaylistStrategy
Modifies the playlist by changing all PlaylistItem locations.

Specified by:
replaceLocations in interface IPlaylistStrategy
Parameters:
locationMap - maps old locations to new locations.


Copyright © 2007-2008. All Rights Reserved.