Beispiel #1
0
        /// <summary>
        /// Plays an audio file
        /// </summary>
        /// <param name="audio">Audio to play out</param>
        /// <returns>true if audio was played, false otherwise</returns>
        private bool Play(PlayAudioTask audioTask)
        {
            if (playing || !Monitor.TryEnter(syncObj))
            {
                return(false);
            }
            if (audioTask == null)
            {
                return(false);
            }

            Audio audio;
            int   remaining;
            bool  complete;

            if ((audio = audioTask.Audio) == null)
            {
                return(false);
            }
            remaining       = audioTask.Loop;
            complete        = false;
            playingFilePath = audioTask.FileName;
            playing         = true;
            playingAudio    = audio;
            lastPlayResult  = PlayOperationResult.Error;

            try
            {
                do
                {
                    complete = false;
                    audio.Play();
                    while (audio.Playing)
                    {
                        if (audio.CurrentPosition >= audio.Duration)
                        {
                            audio.Stop();
                            complete = true;
                            break;
                        }
                        Thread.Sleep(10);
                    }
                } while (complete && ((audioTask.Loop == -1) || (--remaining > 0)));
                lastPlayResult = PlayOperationResult.Succeeded;
            }
            catch
            {
                lastPlayResult = PlayOperationResult.Error;
                return(false);
            }

            try { audio.Dispose(); }
            catch { }

            playingAudio = null;
            playing      = false;
            Monitor.Exit(syncObj);
            OnStopping();
            return(true);
        }
Beispiel #2
0
 /// <summary>
 /// Plays an audio file
 /// </summary>
 /// <param name="audio">Audio to play out</param>
 /// <returns>true if audio was played, false otherwise</returns>
 private bool Play(PlayAudioTask audioTask)
 {
     /*
      * if (playing || !Monitor.TryEnter(syncObj))
      *      return false;
      * if (audioTask == null)
      *      return false;
      *
      * Audio audio;
      * int remaining;
      * bool complete;
      *
      * if ((audio = audioTask.Audio) == null)
      *      return false;
      * remaining = audioTask.Loop;
      * complete = false;
      * playingFilePath = audioTask.FileName;
      * playing = true;
      * playingAudio = audio;
      * lastPlayResult = PlayOperationResult.Error;
      *
      * try
      * {
      *      do
      *      {
      *              complete = false;
      *              audio.Play();
      *              while (audio.Playing)
      *              {
      *                      if (audio.CurrentPosition >= audio.Duration)
      *                      {
      *                              audio.Stop();
      *                              complete = true;
      *                              break;
      *                      }
      *                      Thread.Sleep(10);
      *              }
      *      } while (complete && ((audioTask.Loop == -1) || (--remaining > 0)));
      *      lastPlayResult = PlayOperationResult.Succeeded;
      * }
      * catch
      * {
      *      lastPlayResult = PlayOperationResult.Error;
      *      return false;
      * }
      *
      * try { audio.Dispose(); }
      * catch { }
      *
      * playingAudio = null;
      * playing = false;
      * Monitor.Exit(syncObj);
      * OnStopping();
      * return true;
      */
     return(false);
 }
Beispiel #3
0
        /// <summary>
        /// Plays an audio file synchronously
        /// </summary>
        /// <param name="audioFilePath">Path to the audio file to play</param>
        /// <returns>true if audio file was loaded and enqueued successfully, false otherwise</returns>
        public bool PlaySync(string audioFilePath)
        {
            PlayAudioTask audioTask;

            try
            {
                audioTask = new PlayAudioTask(audioFilePath);
            }
            catch { return(false); }
            return(Play(audioTask));
        }
Beispiel #4
0
        /// <summary>
        /// Plays an audio file asynchronously to the infinity
        /// </summary>
        /// <param name="audioFilePath">Audio object to play</param>
        /// <returns>true if audio file was loaded and enqueued successfully, false otherwise</returns>
        public bool PlayLoop(string audioFilePath)
        {
            if (playQueue.Count >= playQueue.Capacity)
            {
                return(false);
            }
            PlayAudioTask audio;

            try
            {
                audio = new PlayAudioTask(audioFilePath, -1);
            }
            catch { return(false); }
            if (audio != null)
            {
                playQueue.Produce(audio);
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Plays an audio file
        /// </summary>
        /// <param name="audio">Audio to play out</param>
        /// <returns>true if audio was played, false otherwise</returns>
        private bool Play(PlayAudioTask audioTask)
        {
            /*
            if (playing || !Monitor.TryEnter(syncObj))
                return false;
            if (audioTask == null)
                return false;

            Audio audio;
            int remaining;
            bool complete;

            if ((audio = audioTask.Audio) == null)
                return false;
            remaining = audioTask.Loop;
            complete = false;
            playingFilePath = audioTask.FileName;
            playing = true;
            playingAudio = audio;
            lastPlayResult = PlayOperationResult.Error;

            try
            {
                do
                {
                    complete = false;
                    audio.Play();
                    while (audio.Playing)
                    {
                        if (audio.CurrentPosition >= audio.Duration)
                        {
                            audio.Stop();
                            complete = true;
                            break;
                        }
                        Thread.Sleep(10);
                    }
                } while (complete && ((audioTask.Loop == -1) || (--remaining > 0)));
                lastPlayResult = PlayOperationResult.Succeeded;
            }
            catch
            {
                lastPlayResult = PlayOperationResult.Error;
                return false;
            }

            try { audio.Dispose(); }
            catch { }

            playingAudio = null;
            playing = false;
            Monitor.Exit(syncObj);
            OnStopping();
            return true;
            */
            return false;
        }