internal void AddToFront(QueuedPlayableContext queuedPlayableContext)
 {
     lock (m_queuedPlayables)
     {
         m_queuedPlayables.Insert(0, queuedPlayableContext);
     }
 }
 internal void Add(QueuedPlayableContext queuedPlayableContext)
 {
     lock (m_queuedPlayables)
     {
         m_queuedPlayables.Add(queuedPlayableContext);
     }
 }
        internal void FinishedPlaying(QueuedPlayableContext ctxt)
        {
            lock (m_queuedPlayables)
            {
                if (ctxt == CurrentlyPlaying)
                {
                    LastPlayFinish = DateTime.Now;

                    CurrentlyPlaying = null;
                }
            }
        }
        void PlayNextSequentialContent(PlayableContentQueue queue)
        {
            QueuedPlayableContext ctxt = null;

            lock (queue.SyncRoot)
            {
                if (queue.CurrentlyPlaying != null)
                {
                    return;
                }

                ctxt = queue.GetNext();
            }

            Action next = () =>
            {
                if (ctxt.OnClose != null)
                {
                    ctxt.OnClose();
                }

                lock (queue.SyncRoot)
                {
                    queue.FinishedPlaying(ctxt);
                }

                PlayNextSequentialContent(queue);
            };

            if (ctxt != null)
            {
                var playable = ctxt.Playable;

                if (ctxt.OnPlay != null)
                {
                    ctxt.OnPlay(ctxt.ActivationContext, playable, next);
                }
                else if (ctxt.OnClose != null)
                {
                    next();
                }
            }
        }
        protected virtual void AddToQueue(PlayableContentQueue queue, ResourceActivationContext activationContext, PlayableContent playable, PlayContentDelegate onPlay, Action onClose)
        {
            lock (m_queues)
            {
                m_queues.Add(activationContext.InstanceId, queue);
            }

            switch (playable.Priority)
            {
            case PlayableContentPriority.High:
                // Todo: actually want to be a bit more clever and *don't* move in front of
                // other high priority ones.
                queue.AddToFront(
                    new QueuedPlayableContext
                {
                    ActivationContext = activationContext,
                    Playable          = playable,
                    OnClose           = onClose,
                    OnPlay            = onPlay
                });

                PlayNextSequentialContent(queue);
                break;

            case PlayableContentPriority.Interrupt:
                QueuedPlayableContext toStop = null;

                lock (queue.SyncRoot)
                {
                    toStop = queue.CurrentlyPlaying;
                }

                if (toStop != null)
                {
                    StopPlaying(toStop.ActivationContext, toStop.Playable, true);
                    toStop.ActivationContext.Close();
                }

                queue.AddToFront(
                    new QueuedPlayableContext
                {
                    ActivationContext = activationContext,
                    Playable          = playable,
                    OnClose           = onClose,
                    OnPlay            = onPlay
                });

                PlayNextSequentialContent(queue);
                break;

            case PlayableContentPriority.Filler:
                // Only if nothing has played on this queue for a while...
                bool doPlay = false;

                lock (queue.SyncRoot)
                {
                    doPlay = queue.CurrentlyPlaying == null &&
                             (DateTime.Now - queue.LastPlayFinish).TotalSeconds >= MinFillerDelay;
                }

                if (doPlay)
                {
                    queue.Add(
                        new QueuedPlayableContext
                    {
                        ActivationContext = activationContext,
                        Playable          = playable,
                        OnClose           = onClose,
                        OnPlay            = onPlay
                    });

                    PlayNextSequentialContent(queue);
                }
                break;

            case PlayableContentPriority.Normal:
            default:
                queue.Add(
                    new QueuedPlayableContext
                {
                    ActivationContext = activationContext,
                    Playable          = playable,
                    OnClose           = onClose,
                    OnPlay            = onPlay
                });

                PlayNextSequentialContent(queue);
                break;
            }
        }