Self-hatred

[XNA] Muzak II

I did some basic soundtrack-handling last night. I ran into problems getting it to run before I discovered that Content.Load(bar) can only be called from the main class. That was a bit of a pisser, and it forced me to rewrite my class. I am defending its existence on the grounds of potential extensibility.

class Muzak
{
	Song song;

	public Muzak()
	{
	}

	public void Next(Song inputSong)
	{
		// Import song.
		song = inputSong;
		MediaPlayer.Play(song);
	}

	public bool Playing()
	{
		// Is it playing?
		if (MediaPlayer.State == MediaState.Playing)
		return true;
		
		return false;
	}

	public void Pause()
	{

		if (MediaPlayer.State == MediaState.Playing)
		MediaPlayer.Pause();
	}

	public void Resume()
	{
		if (MediaPlayer.State == MediaState.Paused)
		MediaPlayer.Resume();
	}

	public string Name()
	{
		// Song name.
		return song.Name;
	}

	// Parse code stolen from RB Whitaker.
	// URL: http://rbwhitaker.wikidot.com/playing-background-music
	public string Parse(TimeSpan timeSpan)
	{
		int minutes = timeSpan.Minutes;
		int seconds = timeSpan.Seconds;

		if (seconds < 10)
		return minutes + ":0" + seconds;
		else
		return minutes + ":" + seconds;
	}

	// These next four methods all do the same thing in different combinations.
	// While I partially duplicate existing features, I wanted something extensible.
	public string Elapsed()
	{
		return Parse(MediaPlayer.PlayPosition);
	}

	public string Length()
	{
		return Parse(song.Duration);
	}

	public string Time()
	{
		return Elapsed() + " / " + Length();
	}

	public string NameTime()
	{
		return Name() + " " + Time();
	}
}


Categorised as: programming


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>