2012-08-09 09:45:04 +00:00
|
|
|
using System;
|
2012-08-26 10:31:54 +00:00
|
|
|
using System.Collections.Generic;
|
2012-09-29 22:01:15 +00:00
|
|
|
using ANX.Framework.NonXNA.Development;
|
2011-11-17 20:35:25 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-11-17 20:35:25 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework.Media
|
|
|
|
{
|
2012-09-29 22:01:15 +00:00
|
|
|
[PercentageComplete(100)]
|
|
|
|
[Developer("AstrorEnales")]
|
2012-10-10 10:48:19 +00:00
|
|
|
[TestState(TestStateAttribute.TestState.Untested)]
|
2012-01-16 20:08:38 +00:00
|
|
|
public sealed class MediaQueue
|
2011-11-17 20:35:25 +00:00
|
|
|
{
|
2012-09-30 13:51:32 +00:00
|
|
|
private readonly List<Song> queue;
|
|
|
|
private readonly List<Song> shuffledQueue;
|
|
|
|
private int activeSongIndex;
|
2012-08-26 10:31:54 +00:00
|
|
|
|
|
|
|
#region Public
|
2012-09-29 22:01:15 +00:00
|
|
|
public int Count
|
|
|
|
{
|
|
|
|
get { return queue.Count; }
|
|
|
|
}
|
2012-01-16 20:08:38 +00:00
|
|
|
|
2012-09-30 13:51:32 +00:00
|
|
|
public int ActiveSongIndex
|
|
|
|
{
|
|
|
|
get { return activeSongIndex; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (Count <= 0)
|
|
|
|
return;
|
2012-08-26 10:31:54 +00:00
|
|
|
|
2012-09-30 13:51:32 +00:00
|
|
|
ActiveSong.Stop();
|
|
|
|
activeSongIndex = Math.Min(value, queue.Count);
|
|
|
|
ActiveSong.Play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Song ActiveSong
|
2012-09-29 22:01:15 +00:00
|
|
|
{
|
2012-09-30 13:51:32 +00:00
|
|
|
get { return shuffledQueue.Count <= 0 ? null : shuffledQueue[ActiveSongIndex]; }
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
2012-08-26 10:31:54 +00:00
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
public Song this[int index]
|
|
|
|
{
|
2012-09-30 13:51:32 +00:00
|
|
|
get { return shuffledQueue[index]; }
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
|
|
|
#endregion
|
2012-01-16 20:08:38 +00:00
|
|
|
|
2012-08-26 10:31:54 +00:00
|
|
|
#region Constructor
|
|
|
|
internal MediaQueue()
|
2012-01-16 20:08:38 +00:00
|
|
|
{
|
2012-08-26 10:31:54 +00:00
|
|
|
queue = new List<Song>();
|
2012-09-30 13:51:32 +00:00
|
|
|
shuffledQueue = new List<Song>();
|
2012-08-26 10:31:54 +00:00
|
|
|
}
|
2012-09-29 22:01:15 +00:00
|
|
|
|
|
|
|
~MediaQueue()
|
|
|
|
{
|
|
|
|
queue.Clear();
|
2012-09-30 13:51:32 +00:00
|
|
|
shuffledQueue.Clear();
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
2012-08-26 10:31:54 +00:00
|
|
|
#endregion
|
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
#region Play
|
|
|
|
internal void Play(Song song)
|
2012-08-26 10:31:54 +00:00
|
|
|
{
|
|
|
|
if (song == null)
|
|
|
|
throw new ArgumentNullException("song");
|
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
Clear();
|
2012-08-26 10:31:54 +00:00
|
|
|
queue.Add(song);
|
2012-09-30 13:51:32 +00:00
|
|
|
shuffledQueue.Add(song);
|
2012-09-29 22:01:15 +00:00
|
|
|
ActiveSong.Play();
|
2012-08-26 10:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void Play(SongCollection songCollection)
|
|
|
|
{
|
|
|
|
if (songCollection == null)
|
|
|
|
throw new ArgumentNullException("songCollection");
|
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
Clear();
|
|
|
|
queue.AddRange(songCollection);
|
2012-09-30 13:51:32 +00:00
|
|
|
UpdateOrder();
|
2012-09-29 22:01:15 +00:00
|
|
|
ActiveSong.Play();
|
2012-08-26 10:31:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
internal void Play(SongCollection songCollection, int index)
|
|
|
|
{
|
|
|
|
if (songCollection == null)
|
|
|
|
throw new ArgumentNullException("songCollection");
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
ActiveSongIndex = index;
|
|
|
|
queue.AddRange(songCollection);
|
2012-09-30 13:51:32 +00:00
|
|
|
UpdateOrder();
|
2012-09-29 22:01:15 +00:00
|
|
|
ActiveSong.Play();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-09-30 13:51:32 +00:00
|
|
|
internal void UpdateOrder()
|
|
|
|
{
|
|
|
|
if (Count <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Song currentPlayingSong = ActiveSong;
|
|
|
|
if (MediaPlayer.IsShuffled)
|
|
|
|
Shuffle();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shuffledQueue.Clear();
|
|
|
|
shuffledQueue.AddRange(queue.ToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
activeSongIndex = shuffledQueue.IndexOf(currentPlayingSong);
|
|
|
|
}
|
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
private void Clear()
|
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
ActiveSongIndex = 0;
|
|
|
|
queue.Clear();
|
2012-09-30 13:51:32 +00:00
|
|
|
shuffledQueue.Clear();
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void Stop()
|
|
|
|
{
|
|
|
|
if(ActiveSong != null)
|
|
|
|
ActiveSong.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Shuffle()
|
|
|
|
{
|
|
|
|
var rand = new Random();
|
|
|
|
int n = queue.Count;
|
|
|
|
while (n > 1)
|
|
|
|
{
|
|
|
|
int k = rand.Next(n);
|
|
|
|
n--;
|
2012-09-30 13:51:32 +00:00
|
|
|
Song value = shuffledQueue[k];
|
|
|
|
shuffledQueue[k] = shuffledQueue[n];
|
|
|
|
shuffledQueue[n] = value;
|
2012-09-29 22:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region MoveNext
|
2012-10-03 20:47:56 +00:00
|
|
|
internal bool MoveNext(bool isRepeating)
|
2012-08-26 10:31:54 +00:00
|
|
|
{
|
2012-09-29 22:01:15 +00:00
|
|
|
if (Count <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ActiveSong.Stop();
|
|
|
|
|
|
|
|
if (ActiveSongIndex < Count - 1)
|
|
|
|
ActiveSongIndex++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ActiveSongIndex = 0;
|
2012-10-03 20:47:56 +00:00
|
|
|
if (isRepeating == false)
|
2012-09-29 22:01:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActiveSong.Play();
|
|
|
|
return true;
|
2012-01-16 20:08:38 +00:00
|
|
|
}
|
2012-09-29 22:01:15 +00:00
|
|
|
#endregion
|
2012-01-16 20:08:38 +00:00
|
|
|
|
2012-09-29 22:01:15 +00:00
|
|
|
#region MovePrevious
|
|
|
|
internal void MovePrevious()
|
2012-01-16 20:08:38 +00:00
|
|
|
{
|
2012-09-29 22:01:15 +00:00
|
|
|
if (Count <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ActiveSong.Stop();
|
|
|
|
|
|
|
|
if (ActiveSongIndex > 0)
|
|
|
|
ActiveSongIndex--;
|
|
|
|
else
|
|
|
|
ActiveSongIndex = Count - 1;
|
|
|
|
|
|
|
|
ActiveSong.Play();
|
2012-01-16 20:08:38 +00:00
|
|
|
}
|
2012-09-29 22:01:15 +00:00
|
|
|
#endregion
|
2011-11-17 20:35:25 +00:00
|
|
|
}
|
|
|
|
}
|