From c7406163b9af5bcf72f1fb88bf213262f355e5c7 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Fri, 21 Mar 2014 13:15:11 -0400 Subject: [PATCH] Moar Media cleanup, mostly Property junk --- MonoGame.Framework/Media/MediaLibrary.cs | 21 +++-- MonoGame.Framework/Media/MediaPlayer.cs | 89 +++++++++---------- MonoGame.Framework/Media/MediaQueue.cs | 27 +++--- MonoGame.Framework/Media/MediaSource.cs | 22 ++--- MonoGame.Framework/Media/MediaSourceType.cs | 2 - MonoGame.Framework/Media/MediaState.cs | 3 - MonoGame.Framework/Media/Playlist.cs | 11 ++- .../Media/PlaylistCollection.cs | 16 ++-- MonoGame.Framework/Media/SongCollection.cs | 17 ++-- 9 files changed, 101 insertions(+), 107 deletions(-) diff --git a/MonoGame.Framework/Media/MediaLibrary.cs b/MonoGame.Framework/Media/MediaLibrary.cs index 0f3927cb387..d4c2b028062 100644 --- a/MonoGame.Framework/Media/MediaLibrary.cs +++ b/MonoGame.Framework/Media/MediaLibrary.cs @@ -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; } @@ -32,16 +32,16 @@ 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(); } @@ -49,10 +49,13 @@ public SongCollection Songs { get { + /* This is meant to return a pre-made collection, based on the + * WMP library. + * -flibit + */ return new SongCollection(); } } } } - diff --git a/MonoGame.Framework/Media/MediaPlayer.cs b/MonoGame.Framework/Media/MediaPlayer.cs index 0f34ea05c1c..75eaf6f1368 100644 --- a/MonoGame.Framework/Media/MediaPlayer.cs +++ b/MonoGame.Framework/Media/MediaPlayer.cs @@ -8,8 +8,6 @@ #endregion using System; -using Microsoft.Xna.Framework.Audio; -using System.Linq; namespace Microsoft.Xna.Framework.Media { @@ -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 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; } @@ -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); @@ -119,27 +114,31 @@ 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); } } @@ -147,12 +146,12 @@ public static float Volume 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; } @@ -163,33 +162,33 @@ public static void Pause() /// 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; } @@ -197,11 +196,11 @@ private static void PlaySong(Song song) 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(); @@ -225,7 +224,7 @@ public static void Resume() return; } - _queue.ActiveSong.Resume(); + Queue.ActiveSong.Resume(); State = MediaState.Playing; } @@ -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; @@ -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) { diff --git a/MonoGame.Framework/Media/MediaQueue.cs b/MonoGame.Framework/Media/MediaQueue.cs index a9691d6c264..9c42f49bb77 100644 --- a/MonoGame.Framework/Media/MediaQueue.cs +++ b/MonoGame.Framework/Media/MediaQueue.cs @@ -15,36 +15,30 @@ namespace Microsoft.Xna.Framework.Media public sealed class MediaQueue { List songs = new List(); - 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 @@ -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(); @@ -116,4 +110,3 @@ internal void Stop() } } } - diff --git a/MonoGame.Framework/Media/MediaSource.cs b/MonoGame.Framework/Media/MediaSource.cs index 7360a865819..8d348629eed 100644 --- a/MonoGame.Framework/Media/MediaSource.cs +++ b/MonoGame.Framework/Media/MediaSource.cs @@ -7,36 +7,28 @@ */ #endregion -using System; using System.Collections.Generic; namespace Microsoft.Xna.Framework.Media { public sealed class MediaSource { - private MediaSourceType _type; - private string _name; - internal MediaSource(string name, MediaSourceType type) { - _name = name; - _type = type; + Name = name; + MediaSourceType = type; } - public Microsoft.Xna.Framework.Media.MediaSourceType MediaSourceType + public MediaSourceType MediaSourceType { - get - { - return _type; - } + get; + private set; } public string Name { - get - { - return _name; - } + get; + private set; } public static IList GetAvailableMediaSources() diff --git a/MonoGame.Framework/Media/MediaSourceType.cs b/MonoGame.Framework/Media/MediaSourceType.cs index 720fd0b6ccb..956a41334cf 100644 --- a/MonoGame.Framework/Media/MediaSourceType.cs +++ b/MonoGame.Framework/Media/MediaSourceType.cs @@ -7,8 +7,6 @@ */ #endregion -using System; - namespace Microsoft.Xna.Framework.Media { public enum MediaSourceType diff --git a/MonoGame.Framework/Media/MediaState.cs b/MonoGame.Framework/Media/MediaState.cs index 10d53e98db2..50a976ce98c 100644 --- a/MonoGame.Framework/Media/MediaState.cs +++ b/MonoGame.Framework/Media/MediaState.cs @@ -7,8 +7,6 @@ */ #endregion -using System; - namespace Microsoft.Xna.Framework.Media { public enum MediaState @@ -18,4 +16,3 @@ public enum MediaState Paused } } - diff --git a/MonoGame.Framework/Media/Playlist.cs b/MonoGame.Framework/Media/Playlist.cs index f5b8c521a85..06955181c92 100644 --- a/MonoGame.Framework/Media/Playlist.cs +++ b/MonoGame.Framework/Media/Playlist.cs @@ -16,13 +16,19 @@ public sealed class Playlist : IDisposable public TimeSpan Duration { get; - internal set; + private set; } public string Name { get; - internal set; + private set; + } + + internal Playlist(TimeSpan duration, string name) + { + Duration = duration; + Name = name; } public void Dispose() @@ -30,4 +36,3 @@ public void Dispose() } } } - diff --git a/MonoGame.Framework/Media/PlaylistCollection.cs b/MonoGame.Framework/Media/PlaylistCollection.cs index 1ac9d5063cd..e3a34c5dd13 100644 --- a/MonoGame.Framework/Media/PlaylistCollection.cs +++ b/MonoGame.Framework/Media/PlaylistCollection.cs @@ -16,11 +16,17 @@ namespace Microsoft.Xna.Framework.Media public sealed class PlaylistCollection : ICollection, IEnumerable, IEnumerable, IDisposable { - private bool isReadOnly = false; - private List innerlist = new List(); + private List innerlist; + + internal PlaylistCollection() + { + IsReadOnly = false; + innerlist = new List(); + } public void Dispose() { + innerlist.Clear(); } public IEnumerator GetEnumerator() @@ -43,10 +49,8 @@ public int Count public bool IsReadOnly { - get - { - return isReadOnly; - } + get; + private set; } public Playlist this[int index] diff --git a/MonoGame.Framework/Media/SongCollection.cs b/MonoGame.Framework/Media/SongCollection.cs index 6d0201f585d..c6a16f25328 100644 --- a/MonoGame.Framework/Media/SongCollection.cs +++ b/MonoGame.Framework/Media/SongCollection.cs @@ -15,11 +15,17 @@ namespace Microsoft.Xna.Framework.Media { public class SongCollection : ICollection, IEnumerable, IEnumerable, IDisposable { - private bool isReadOnly = false; - private List innerlist = new List(); + private List innerlist; + + internal SongCollection() + { + IsReadOnly = false; + innerlist = new List(); + } public void Dispose() { + innerlist.Clear(); } public IEnumerator GetEnumerator() @@ -42,10 +48,8 @@ public int Count public bool IsReadOnly { - get - { - return isReadOnly; - } + get; + private set; } public Song this[int index] @@ -118,4 +122,3 @@ public bool Remove(Song item) } } } -