Skip to content
Browse files

Added more functionality to Song and media player. Songs on iOS now s…

…tream from the file rather than loading into memory.
  • Loading branch information...
1 parent 326b9c5 commit 76bd6674f70ab9565e73877a7a223afed1ccb018 @RayBatts RayBatts committed May 14, 2012
View
6 MonoGame.Framework/Content/ContentReaders/SongReader.cs
@@ -66,14 +66,10 @@ protected internal override Song Read(ContentReader input, Song existingInstance
// Songs don't have the full directory path in their .xnb. Build it.
path = Path.Combine (input.ContentManager.RootDirectory, input.ContentManager.CurrentAssetDirectory, path);
path = TitleContainer.GetFilename(path);
-
- Song song;
- using (var fs = TitleContainer.OpenStream(path))
- song = new Song(path, fs);
/*int durationMS =*/ input.ReadObject<int>();
- return song;
+ return new Song(path);
}
}
}
View
26 MonoGame.Framework/Media/MediaPlayer.cs
@@ -59,7 +59,6 @@ public static class MediaPlayer
private static int _numSongsInQueuePlayed = 0;
private static MediaState _mediaState = MediaState.Stopped;
private static float _volume = 1.0f;
- private static bool _looping = true;
private static bool _isMuted = false;
private static MediaQueue _queue = new MediaQueue();
@@ -169,13 +168,27 @@ public static void Play(Song song)
_queue.Add(song);
+ playSong(song);
+ }
+
+ public static void Play(SongCollection collection, int index = 0)
+ {
+ foreach(var song in collection)
+ Queue.Add(song);
+
+ _queue.ActiveSongIndex = index;
+
+ playSong(Queue[index]);
+ }
+
+ private static void playSong(Song song)
+ {
song.SetEventHandler(OnSongFinishedPlaying);
song.Volume = _isMuted ? 0.0f : _volume;
- //song.Loop = _looping;
song.Play();
_mediaState = MediaState.Playing;
- }
+ }
internal static void OnSongFinishedPlaying (object sender, EventArgs args)
{
@@ -192,7 +205,7 @@ internal static void OnSongFinishedPlaying (object sender, EventArgs args)
}
}
- nextSong(1);
+ MoveNext();
}
public static void Resume()
@@ -209,7 +222,10 @@ public static void Stop()
if (_queue.ActiveSong == null)
return;
- _queue.ActiveSong.Stop();
+ // Loop through so that we reset the PlayCount as well
+ foreach(var song in Queue)
+ _queue.ActiveSong.Stop();
+
_mediaState = MediaState.Stopped;
}
View
21 MonoGame.Framework/Media/MediaQueue.cs
@@ -13,7 +13,6 @@ public MediaQueue () : base()
}
-
public Song ActiveSong
{
get
@@ -35,34 +34,22 @@ internal Song getNextSong(int direction, bool shuffle)
{
if (shuffle)
_activeSongIndex = random.Next(this.Count);
- else
- {
- _activeSongIndex += direction;
-
- if (_activeSongIndex >= Count)
- _activeSongIndex = 0;
- else if (_activeSongIndex < 0)
- _activeSongIndex = Count - 1;
- }
+ else
+ _activeSongIndex = (int)MathHelper.Clamp(_activeSongIndex + direction, 0, Count - 1);
return this[_activeSongIndex];
}
internal new void Clear ()
{
Song song;
- for( int x = 0; x < this.Count; x++ )
+ for(; this.Count > 0; )
{
- song = this[x];
+ song = this[0];
song.Stop();
this.Remove(song);
}
}
-
- internal void queueSong(Song song)
- {
- this.Add(song);
- }
}
}
View
66 MonoGame.Framework/Media/Song.cs
@@ -69,28 +69,14 @@ internal Song(string fileName)
_name = fileName;
#if IPHONE
- SongData = NSData.FromFile(fileName);
- _sound = AVAudioPlayer.FromData(SongData);
+ _sound = AVAudioPlayer.FromUrl(NSUrl.FromFilename(fileName));
_sound.NumberOfLoops = 0;
#else
_sound = new SoundEffect(_name).CreateInstance();
#endif
_sound.FinishedPlaying += OnFinishedPlaying;
}
-#if IPHONE
-
- internal Song(string fileName, Stream dataStream)
- {
- _name = fileName;
-
- SongData = NSData.FromStream(dataStream);
- _sound = AVAudioPlayer.FromData(SongData);
- _sound.NumberOfLoops = 0;
- _sound.FinishedPlaying += OnFinishedPlaying;
- }
-#endif
-
internal void OnFinishedPlaying (object sender, EventArgs args)
{
if (DonePlaying == null)
@@ -115,8 +101,6 @@ public string FilePath
get { return _name; }
}
- public NSData SongData { get; private set; }
-
public void Dispose()
{
_sound.Dispose();
@@ -189,67 +173,32 @@ internal void Pause()
internal void Stop()
{
- if ( _sound != null )
+ if ( _sound == null )
return;
_sound.Stop();
- }
-
- internal bool Loop
- {
- get
- {
- if ( _sound == null )
- return false;
-
-#if IPHONE
- return _sound.NumberOfLoops > 0;
-#else
- return _sound.IsLooped;
-#endif
-
- }
- set
- {
- if ( _sound == null )
- return;
-#if IPHONE
- _sound.NumberOfLoops = value ? int.MaxValue : 0;
-#else
- if ( _sound.IsLooped == value )
- return;
-
- _sound.IsLooped = value;
-#endif
- }
+
+ _playCount = 0;
}
internal float Volume
{
get
{
if (_sound != null)
- {
return _sound.Volume;
- }
else
- {
return 0.0f;
- }
}
set
{
- if ( _sound != null )
- {
- if ( _sound.Volume != value )
- {
- _sound.Volume = value;
- }
- }
+ if ( _sound != null && _sound.Volume != value )
+ _sound.Volume = value;
}
}
+ // TODO: Implement
public TimeSpan Duration
{
get
@@ -267,6 +216,7 @@ public TimeSpan Duration
}
}
+ // TODO: Implement
public TimeSpan Position
{
get

0 comments on commit 76bd667

Please sign in to comment.
Something went wrong with that request. Please try again.