Esempio n. 1
0
 //Stop without next song
 public bool stopSound()
 {
     PlaybackStopType = PlaybackStopTypes.PlaybackStoppedByUser;
     if (playing)
     {
         outputDevice?.Dispose();
         outputDevice = null;
         try
         {
             audioFile?.Dispose();
         }
         catch (Exception ex)
         {
             Debug.Print("exception: " + ex.ToString());
         }
         audioFile = null;
         playing   = false;
         return(true);
     }
     else
     {
         outputDevice?.Stop();
         audioFile?.Dispose();
         audioFile = null;
         return(false);
     }
 }
        /// <summary>
        /// Initialize a local audio player with given type
        /// </summary>
        /// <param name="fileName">the file name to play</param>
        /// <param name="waveOutType">the wave out type to use</param>
        public LocalAudioPlayer(string fileName, WaveOutType waveOutType)
        {
            this.fileName    = fileName;
            PlaybackStopType = PlaybackStopTypes.PlaybackStoppedReachingEndOfFile;
            Debug.Assert(this.wavePlayer == null);
            this.wavePlayer             = CreateWavePlayer(waveOutType);
            wavePlayer.PlaybackStopped += wavePlayer_PlaybackStopped;
            try
            {
                if (fileName.ToLowerInvariant().EndsWith(".ogg"))
                {
                    vorbisReader = new VorbisWaveReader(fileName);
                    wavePlayer.Init(vorbisReader);
                }
                else if (fileName.ToLowerInvariant().EndsWith(".flac"))
                {
                    flacReader = new FlacReader(fileName);
                    wavePlayer.Init(flacReader);
                }
                else
                {
                    this.file = new AudioFileReader(fileName);
                    this.wavePlayer.Init(file);
                }

                //this.wavePlayer.PlaybackStopped += wavePlayer_PlaybackStopped;
            }
            catch (Exception)
            {
                //throw;
            }

            //Play();
        }
Esempio n. 3
0
 public AudioPlayer(string filepath)
 {
     PlaybackStopType        = PlaybackStopTypes.PlaybackStoppedReachingEndOfFile;
     output                  = new WaveOut();
     output.PlaybackStopped += _output_PlaybackStopped;
     _audioFileReader        = new Mp3FileReader(filepath);
     output.Init(_audioFileReader);
 }
Esempio n. 4
0
 public void playSoundForced(String filePath)
 {
     audioFile = new AudioFileReader(filePath);
     outputDevice.Init(audioFile);
     outputDevice.Play();
     //Debug.Print("VOLUME FORCED: " + (volume / 20f).ToString());
     outputDevice.Volume = volume / 20f;
     PlaybackStopType    = PlaybackStopTypes.PlaybackStoppedReachingEndOfFile;
 }
Esempio n. 5
0
        public void Stop()
        {
            if (output != null)
            {
                output.Pause();
                this.SetPosition(0);
                PlaybackStopType = PlaybackStopTypes.PlaybackStoppedByUser;

                if (PlaybackStopped != null)
                {
                    PlaybackStopped();
                }
            }
        }
Esempio n. 6
0
        public AudioPlayer(string filepath, float volume)
        {
            PlaybackStopType = PlaybackStopTypes.PlaybackStoppedReachingEndOfFile;

            _audioFileReader = new AudioFileReader(filepath)
            {
                Volume = volume
            };

            _output = new DirectSoundOut(200);
            _output.PlaybackStopped += _output_PlaybackStopped;

            var wc = new WaveChannel32(_audioFileReader);

            wc.PadWithZeroes = false;

            _output.Init(wc);
        }
Esempio n. 7
0
 public bool playSound(String filePath)
 {
     if (playing)
     {
         if (outputDevice == null)
         {
             outputDevice = new WaveOutEvent();
             outputDevice.PlaybackStopped += OnPlaybackStopped;
         }
         if (audioFile == null)
         {
             try
             {
                 audioFile = new AudioFileReader(filePath);
                 outputDevice.Init(audioFile);
             }
             catch (FormatException)
             {
                 MessageBox.Show("Format exception, file not supported!");
                 return(false);
             }
         }
         try
         {
             outputDevice.Play();
             outputDevice.Volume = volume / 20f;
             //Debug.Print("VOLUME: " + (volume / 20f).ToString());
             PlaybackStopType = PlaybackStopTypes.PlaybackStoppedReachingEndOfFile;
             return(true);
         }
         catch (InvalidOperationException ex)
         {
             Debug.Print("playSound() InvalidOperationException : " + ex.ToString());
             return(false);
         }
     }
     return(false);
 }
Esempio n. 8
0
        /// <summary>
        /// init the audio player
        /// </summary>
        public void InitPlayer(bool dispose = true, PlaybackStopTypes pbStopType = PlaybackStopTypes.PlaybackStoppedReachingEndOfFile)
        {
            if (aTimer.Enabled)
            {
                aTimer.Stop();
                aTimer.Enabled = false;
            }

            Action action;

            //action = () =>
            //    {
            try
            {
                if (audioPlayer != null)
                {
                    //waitHandle.Set();
                    if (audioPlayer.IsPlaying() || audioPlayer.IsPaused())
                    {
                        audioPlayer.StopPlayback();
                    }
                    while (isWaitingHandle)
                    {
                    }
                    audioPlayer.PlaybackResumed -= audioPlayer_PlaybackResumed;
                    //audioPlayer.PlaybackStopped -= audioPlayer_PlaybackStopped;
                    if (dispose)
                    {
                        audioPlayer.Dispose();
                        audioPlayer = null;
                    }
                }
            }
            catch (Exception)
            {
                //throw;
            }
            //    };
            //Invoke(action);
            try
            {
                //if (audioPlayer == null)

                audioPlayer =
                    new LocalAudioPlayer(mAudioFile, GetSelectedOutputDriver());
                audioPlayer.PlaybackStopType = pbStopType;
                //audioPlayer.PlaybackPaused += audioPlayer_PlaybackPaused;
                audioPlayer.PlaybackResumed += audioPlayer_PlaybackResumed;
                //audioPlayer.PlaybackStopped += audioPlayer_PlaybackStopped;
                if (!IsPlaylistRunning)
                {
                    currentAudioFile = new AudioFileInfo(Path.GetFileName(mAudioFile), mAudioFile);
                }
            }
            catch (Exception)
            {
                action = () => MessageBox.Show(this, string.Format("Cannot load file '{0}'", mAudioFile));
                Invoke(action);
            }
            if (audioPlayer != null)
            {
                action = () =>
                {
                    if (tbVolume.Value != VOL_INIT * 100)
                    {
                        audioPlayer.SetVolume((float)tbVolume.Value / 100f);
                    }
                    else
                    {
                        audioPlayer.SetVolume(VOL_INIT);
                    }
                };
                tbVolume.Invoke(action);
                action = () => lblTotalTime.Text = Utils.FormatTimeSpan(audioPlayer.GetTotalTime());
                lblTotalTime.Invoke(action);
                action = () =>
                {
                    tbAudioPos.Value   = 0;
                    tbAudioPos.Maximum = (int)(audioPlayer.GetTotalTime().Minutes) * 60
                                         + audioPlayer.GetTotalTime().Seconds;
                    if (tbAudioPos.Maximum > 0)
                    {
                        tbAudioPos.Enabled = true;
                    }
                    else
                    {
                        tbAudioPos.Enabled = false;
                    }
                };
                tbAudioPos.Invoke(action);
                action = () =>
                {
                    UpdateMarquee();
                };
                Invoke(action);
            }
        }