Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Commit

Permalink
Moar Media cleanup, mostly Property junk
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Mar 21, 2014
1 parent 741baf3 commit c740616
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 107 deletions.
21 changes: 12 additions & 9 deletions MonoGame.Framework/Media/MediaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#endregion

using System;
using System.Collections.Generic;
using System.IO;

namespace Microsoft.Xna.Framework.Media
{
public class MediaLibrary : IDisposable
{
private PlaylistCollection PlayLists {
private PlaylistCollection PlayLists
{
get;
set;
}
Expand All @@ -32,27 +32,30 @@ public void Dispose()
{
}

public void SavePicture (string name, byte[] imageBuffer)
public void SavePicture(string name, byte[] imageBuffer)
{
// Only is relivant on mobile devices, should throw error on desktops.
throw new NotSupportedException ();
// On XNA4, this fails on Windows/Xbox. Only Phone is supported.
throw new NotSupportedException();
}

public void SavePicture (string name, Stream source)
public void SavePicture(string name, Stream source)
{
// Only is relivant on mobile devices, should throw error on desktops.
throw new NotSupportedException ();
// On XNA4, this fails on Windows/Xbox. Only Phone is supported.
throw new NotSupportedException();
}


public SongCollection Songs
{
get
{
/* This is meant to return a pre-made collection, based on the
* WMP library.
* -flibit
*/
return new SongCollection();
}
}

}
}

89 changes: 44 additions & 45 deletions MonoGame.Framework/Media/MediaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#endregion

using System;
using Microsoft.Xna.Framework.Audio;
using System.Linq;

namespace Microsoft.Xna.Framework.Media
{
Expand All @@ -18,50 +16,46 @@ public static class MediaPlayer
/* Need to hold onto this to keep track of how many songs
* have played when in shuffle mode.
*/
private static int _numSongsInQueuePlayed = 0;
private static MediaState _state = MediaState.Stopped;
private static float _volume = 1.0f;
private static bool _isMuted = false;
private static readonly MediaQueue _queue = new MediaQueue();
private static int numSongsInQueuePlayed = 0;

public static event EventHandler<EventArgs> ActiveSongChanged;

static MediaPlayer()
{
Queue = new MediaQueue();
}

#region Properties

public static MediaQueue Queue
{
get
{
return _queue;
}
get;
private set;
}

private static bool INTERNAL_isMuted = false;
public static bool IsMuted
{
get
{
return _isMuted;
return INTERNAL_isMuted;
}

set
{
_isMuted = value;
INTERNAL_isMuted = value;

if (_queue.Count == 0)
if (Queue.Count == 0)
{
return;
}

float newVolume = value ? 0.0f : _volume;
_queue.SetVolume(newVolume);
Queue.SetVolume(value ? 0.0f : Volume);
}
}

public static bool IsRepeating {
public static bool IsRepeating
{
get;
set;
}
Expand All @@ -84,27 +78,28 @@ public static TimeSpan PlayPosition
{
get
{
if (_queue.ActiveSong == null)
if (Queue.ActiveSong == null)
{
return TimeSpan.Zero;
}

return _queue.ActiveSong.Position;
return Queue.ActiveSong.Position;
}
}

private static MediaState INTERNAL_state = MediaState.Stopped;
public static MediaState State
{
get
{
return _state;
return INTERNAL_state;
}

private set
{
if (_state != value)
if (INTERNAL_state != value)
{
_state = value;
INTERNAL_state = value;
if (MediaStateChanged != null)
{
MediaStateChanged(null, EventArgs.Empty);
Expand All @@ -119,40 +114,44 @@ public static bool GameHasControl
{
get
{
// TODO: Fix me!
/* This is based on whether or not the player is playing custom
* music, rather than yours.
* -flibit
*/
return true;
}
}

private static float INTERNAL_volume = 1.0f;
public static float Volume
{
get
{
return _volume;
return INTERNAL_volume;
}
set
{
_volume = value;
INTERNAL_volume = value;

if (_queue.ActiveSong == null)
if (Queue.ActiveSong == null)
{
return;
}

_queue.SetVolume(_isMuted ? 0.0f : value);
Queue.SetVolume(IsMuted ? 0.0f : value);
}
}

#endregion

public static void Pause()
{
if (State != MediaState.Playing || _queue.ActiveSong == null)
if (State != MediaState.Playing || Queue.ActiveSong == null)
{
return;
}

_queue.ActiveSong.Pause();
Queue.ActiveSong.Pause();

State = MediaState.Paused;
}
Expand All @@ -163,45 +162,45 @@ public static void Pause()
/// </summary>
public static void Play(Song song)
{
_queue.Clear();
_numSongsInQueuePlayed = 0;
_queue.Add(song);
_queue.ActiveSongIndex = 0;
Queue.Clear();
numSongsInQueuePlayed = 0;
Queue.Add(song);
Queue.ActiveSongIndex = 0;

PlaySong(song);
}

public static void Play(SongCollection collection, int index = 0)
{
_queue.Clear();
_numSongsInQueuePlayed = 0;
Queue.Clear();
numSongsInQueuePlayed = 0;

foreach (Song song in collection)
{
_queue.Add(song);
Queue.Add(song);
}

_queue.ActiveSongIndex = index;
Queue.ActiveSongIndex = index;

PlaySong(_queue.ActiveSong);
PlaySong(Queue.ActiveSong);
}

private static void PlaySong(Song song)
{
song.SetEventHandler(OnSongFinishedPlaying);
song.Volume = _isMuted ? 0.0f : _volume;
song.Volume = IsMuted ? 0.0f : Volume;
song.Play();
State = MediaState.Playing;
}

internal static void OnSongFinishedPlaying(object sender, EventArgs args)
{
// TODO: Check args to see if song sucessfully played.
_numSongsInQueuePlayed += 1;
numSongsInQueuePlayed += 1;

if (_numSongsInQueuePlayed >= _queue.Count)
if (numSongsInQueuePlayed >= Queue.Count)
{
_numSongsInQueuePlayed = 0;
numSongsInQueuePlayed = 0;
if (!IsRepeating)
{
Stop();
Expand All @@ -225,7 +224,7 @@ public static void Resume()
return;
}

_queue.ActiveSong.Resume();
Queue.ActiveSong.Resume();
State = MediaState.Playing;
}

Expand All @@ -239,7 +238,7 @@ public static void Stop()
// Loop through so that we reset the PlayCount as well.
foreach (Song song in Queue.Songs)
{
_queue.ActiveSong.Stop();
Queue.ActiveSong.Stop();
}

State = MediaState.Stopped;
Expand All @@ -257,7 +256,7 @@ public static void MovePrevious()

private static void NextSong(int direction)
{
Song nextSong = _queue.GetNextSong(direction, IsShuffled);
Song nextSong = Queue.GetNextSong(direction, IsShuffled);

if (nextSong == null)
{
Expand Down
27 changes: 10 additions & 17 deletions MonoGame.Framework/Media/MediaQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,30 @@ namespace Microsoft.Xna.Framework.Media
public sealed class MediaQueue
{
List<Song> songs = new List<Song>();
private int _activeSongIndex = -1;
private Random random = new Random();

public MediaQueue()
{
ActiveSongIndex = -1;
}

public Song ActiveSong
{
get
{
if (songs.Count == 0 || _activeSongIndex < 0)
if (songs.Count == 0 || ActiveSongIndex < 0)
{
return null;
}

return songs[_activeSongIndex];
return songs[ActiveSongIndex];
}
}

public int ActiveSongIndex
{
get
{
return _activeSongIndex;
}
set
{
_activeSongIndex = value;
}
get;
set;
}

internal int Count
Expand All @@ -67,24 +61,24 @@ internal Song GetNextSong(int direction, bool shuffle)
{
if (shuffle)
{
_activeSongIndex = random.Next(songs.Count);
ActiveSongIndex = random.Next(songs.Count);
}
else
{
_activeSongIndex = (int) MathHelper.Clamp(
_activeSongIndex + direction,
ActiveSongIndex = (int) MathHelper.Clamp(
ActiveSongIndex + direction,
0,
songs.Count - 1
);
}

return songs[_activeSongIndex];
return songs[ActiveSongIndex];
}

internal void Clear()
{
Song song;
for(; songs.Count > 0; )
while (songs.Count > 0)
{
song = songs[0];
song.Stop();
Expand Down Expand Up @@ -116,4 +110,3 @@ internal void Stop()
}
}
}

Loading

0 comments on commit c740616

Please sign in to comment.