Exemplo n.º 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);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Cancels current audio play operations
 /// </summary>
 public void Stop()
 {
     if (playingAudio != null)
     {
         try
         {
             lock (playingAudio)
             {
                 playingAudio.Pause();
                 playingAudio.Stop();
             }
         }
         catch { }
         lastPlayResult = PlayOperationResult.Canceled;
     }
 }