Skip to main content

Service reference

Namespace: WebRadioFM.Services.

AudioPlayerService : IDisposable​

Adapts XNA local music APIs to an event-based page service.

Events​

EventDelegatePayload
TrackChangedAction<Track>Current copied track metadata
PlayStateChangedAction<bool>Whether service considers player active
ProgressChangedAction<TimeSpan>Current XNA play position
PlaylistEndedActionDeclared but never raised

Properties​

PropertyTypeAccessDescription
CurrentTrackTrackgetValid indexed item or null
IsPlayingboolgetMirrored play state
IsShuffledboolget/setChanges Next selection algorithm
PlaylistList<Track>getExposes mutable internal list
PositionTimeSpangetMediaPlayer.PlayPosition
DurationTimeSpangetCurrent copied track duration or zero

Methods​

SignatureBehavior
AudioPlayerService()Initializes fields/library and subscribes static XNA events
void LoadSongsFromMusicLibrary()Rebuilds the sorted copied playlist and aligned XNA song map; sets index 0 when non-empty
void Play(int index = -1)Optionally selects a valid mapped index; dispatches XNA play and service events
void Pause()Pauses XNA and publishes false
void Resume()Resumes XNA and publishes true
void Stop()Stops XNA and publishes false
void PlayNext()Random index in shuffle mode, otherwise increment/wrap; then Play()
void PlayPrevious()Restarts after 3 seconds, otherwise decrement/wrap; then Play()
void UpdateProgress()Publishes current position only when _isPlaying
List<Track> GetTracksByArtist(string artist)Case-insensitive exact match, returning a new list
void Dispose()Unsubscribes static media events once

Private handlers​

  • OnMediaStateChanged mirrors MediaPlayer.State == MediaState.Playing, then publishes state and progress.
  • OnActiveSongChanged searches the aligned song map by identity, sets the corresponding shared index, and publishes track/state; exceptions are ignored.

Play catches playback exceptions and presents MessageBox.Show.

LastFMService​

Callback-based Last.fm API client and session store.

Properties​

PropertyTypeAccessDescription
SessionLastFmSessiongetCurrent session object
IsAuthenticatedboolgetSession exists and has a key
ApiKeystringget/setIn-memory API key
ApiSecretstringget/setIn-memory signature secret

Configuration/authentication​

void Configure(string apiKey, string apiSecret)​

Assigns credentials without validation or persistence.

string GetAuthUrl(string token)​

Formats Last.fm authorization URL from base URL, _apiKey, and token. Values are not explicitly escaped in this method.

void GetTokenAsync(Action<string> callback, Action<string> errorCallback)​

Signs/calls auth.gettoken. Success payload is token. Fails on API error or parser returning empty.

void GetSessionAsync(string token, Action<LastFmSession> callback, Action<string> errorCallback)​

Signs/calls auth.getSession. On valid session, replaces _session, persists it, then invokes success.

Track mutations​

All methods first reject an unauthenticated session with Not authenticated with Last.fm.

SignatureAPI methodOptional fields
UpdateNowPlayingAsync(string artist, string track, string album, int duration, Action callback, Action<string> errorCallback)track.updateNowPlayingalbum when non-empty
ScrobbleAsync(string artist, string track, string album, int duration, DateTime timestamp, Action callback, Action<string> errorCallback)track.scrobblealbum when non-empty
LoveTrackAsync(string artist, string track, Action callback, Action<string> errorCallback)track.lovenone
UnloveTrackAsync(string artist, string track, Action callback, Action<string> errorCallback)track.unlovenone

All include method, API key, session key, artist/track, and signature. Scrobble adds Unix timestamp and duration; Now Playing adds duration.

Read methods​

SignatureReturn callbackAPI method
GetRecentTracksAsync(string username, int limit, Action<List<RecentTrackItem>> callback, Action<string> errorCallback)list or empty listuser.getRecentTracks
GetTopArtistsAsync(string username, int limit, string period, Action<List<TopArtistItem>> callback, Action<string> errorCallback)list or empty listuser.getTopArtists
GetTopTracksAsync(string username, int limit, string period, Action<List<TopTrackItem>> callback, Action<string> errorCallback)list or empty listuser.getTopTracks
GetTrackInfoAsync(string artist, string track, Action<TrackInfoData> callback, Action<string> errorCallback)track infotrack.getInfo

GetTrackInfoAsync adds session username when non-empty but does not require authentication.

Session persistence​

MethodBehavior
LoadSession()Reads key and username if present; suppresses errors
SaveSession()Private; writes key and username, then saves; suppresses errors
ClearSession()Replaces session, removes both keys, saves; suppresses errors

Request internals​

MethodDescription
BuildRequestUrl(Dictionary<string,string>)Escapes every pair into API URL and appends format=json
MakeRequest(...)Enforces four-request cap, creates/configures HttpWebRequest, writes POST or begins response
HandleResponse(IAsyncResult)Reads JSON, decrements count, dispatches success/error
GetErrorFromJson(string)Returns formatted nonzero API error or null
ParseToken(string)Returns deserialized token or null
ParseSession(string)Maps session response into LastFmSession or null
ToUnixTimestamp(DateTime)Converts UTC difference from Unix epoch, flooring seconds

Nested RequestState​

Private callback state with Request, Callback, ErrorCallback, and OnComplete auto-properties.