private void buttonPause_Click(object sender, EventArgs e)
 {
     if (playbackState == StreamingPlaybackState.Playing || playbackState == StreamingPlaybackState.Buffering)
     {
         waveOut.Pause();
         Debug.WriteLine(String.Format("User requested Pause, waveOut.PlaybackState={0}", waveOut.PlaybackState));
         playbackState = StreamingPlaybackState.Paused;
     }
 }
Exemple #2
0
 private void buttonPlay_Click(object sender, EventArgs e)
 {
     if (playbackState == StreamingPlaybackState.Stopped)
     {
         playbackState        = StreamingPlaybackState.Buffering;
         bufferedWaveProvider = null;
         ThreadPool.QueueUserWorkItem(StreamMp3, textBoxStreamingUrl.Text);
         timer1.Enabled = true;
     }
     else if (playbackState == StreamingPlaybackState.Paused)
     {
         playbackState = StreamingPlaybackState.Buffering;
     }
 }
 private void buttonPlay_Click(object sender, EventArgs e)
 {
     if (playbackState == StreamingPlaybackState.Stopped)
     {
         playbackState = StreamingPlaybackState.Buffering;
         this.bufferedWaveProvider = null;
         ThreadPool.QueueUserWorkItem(new WaitCallback(StreamMP3), textBoxStreamingUrl.Text);
         timer1.Enabled = true;
     }
     else if (playbackState == StreamingPlaybackState.Paused)
     {
         playbackState = StreamingPlaybackState.Buffering;
     }
 }
Exemple #4
0
        public void Start(IAudioClient a_Client)
        {
            if (playbackState == StreamingPlaybackState.Stopped)
            {
                playbackState = StreamingPlaybackState.Buffering;

                _client = a_Client;

                Task.Run(() => { Stream(a_Client); });
            }
            else if (playbackState == StreamingPlaybackState.Paused)
            {
                playbackState = StreamingPlaybackState.Buffering;
            }
        }
 private void Pause()
 {
     if (streamdois == false)
     {
         estadodoplay1 = StreamingPlaybackState.Buffering;
         saidadeaudiowave1.Pause();
         // Debug.WriteLine(String.Format("Paused to buffer, waveOut.PlaybackState={0}", waveOut.PlaybackState));
     }
     if (streamdois == true)
     {
         estadodoplay2 = StreamingPlaybackState.Buffering;
         saidadeaudiowave2.Pause();
         // Debug.WriteLine(String.Format("Paused to buffer, waveOut.PlaybackState={0}", waveOut.PlaybackState));
     }
 }
 private void Play()
 {
     if (streamdois == false)
     {
         saidadeaudiowave1.Play();
         // Debug.WriteLine(String.Format("Started playing, waveOut.PlaybackState={0}", waveOut.PlaybackState));
         estadodoplay1 = StreamingPlaybackState.Playing;
     }
     if (streamdois == true)
     {
         saidadeaudiowave2.Play();
         // Debug.WriteLine(String.Format("Started playing, waveOut.PlaybackState={0}", waveOut.PlaybackState));
         estadodoplay2 = StreamingPlaybackState.Playing;
     }
 }
        private void StopPlayback()
        {
            if (m_PlaybackState != StreamingPlaybackState.Stopped)
            {
                m_PlaybackState = StreamingPlaybackState.Stopped;
                if (m_ASound.WOut != null)
                {
                    m_ASound.WOut.Stop();
                    m_ASound.WOut.Dispose();
                    m_ASound.WOut = null;
                }

                m_ASound.PlayTimer.Stop();
            }
        }
 public void Stop()
 {
     if (playbackState != StreamingPlaybackState.Stopped)
     {
         this.playbackState = StreamingPlaybackState.Stopped;
         StopAndDisposeWaveOut();
         // n.b. streaming thread may not yet have exited
         Thread.Sleep(500);
         this.bufferedWaveProvider = null;
         timer1.Enabled            = false;
         if (OnPlaybackStopped != null)
         {
             OnPlaybackStopped();
         }
     }
 }
        private void StopPlayback()
        {
            if (streamdois == false)
            {
                if (estadodoplay1 != StreamingPlaybackState.Stopped)
                {
                    if (!fullyDownloaded1)
                    {
                        requisicaodaweb1.Abort();
                    }

                    estadodoplay1 = StreamingPlaybackState.Stopped;
                    if (saidadeaudiowave1 != null)
                    {
                        saidadeaudiowave1.Stop();
                        saidadeaudiowave1.Dispose();
                        saidadeaudiowave1 = null;
                    }
                    tgA1.Enabled = false;
                    // n.b. streaming thread may not yet have exited
                    System.Threading.Thread.Sleep(500);
                    ShowBufferState(0);
                }
            }
            if (streamdois == true)
            {
                if (estadodoplay2 != StreamingPlaybackState.Stopped)
                {
                    if (!fullyDownloaded2)
                    {
                        requisicaodaweb2.Abort();
                    }

                    estadodoplay2 = StreamingPlaybackState.Stopped;
                    if (saidadeaudiowave2 != null)
                    {
                        saidadeaudiowave2.Stop();
                        saidadeaudiowave2.Dispose();
                        saidadeaudiowave2 = null;
                    }
                    tgA2.Enabled = false;
                    // n.b. streaming thread may not yet have exited
                    System.Threading.Thread.Sleep(500);
                    ShowBufferState(0);
                }
            }
        }
Exemple #10
0
        protected override void OnTick(object sender, EventArgs eventArgs)
        {
            try
            {
                if (_playbackState != StreamingPlaybackState.Stopped)
                {
                    UpdatePosition();

                    if (WavePlayer == null && _bufferedWaveProvider != null)
                    {
                        Debug.WriteLine("Creating WaveOut Device");
                        CreatePlayer();
                        VolumeProvider = new VolumeWaveProvider16(_bufferedWaveProvider)
                        {
                            Volume = Volume / 100
                        };
                        WavePlayer.Init(VolumeProvider);
                    }
                    else if (_bufferedWaveProvider != null)
                    {
                        var bufferedSeconds = _bufferedWaveProvider.BufferedDuration.TotalSeconds;
                        ShowBufferState(bufferedSeconds);
                        // make it stutter less if we buffer up a decent amount before playing
                        if (bufferedSeconds < 0.5 && _playbackState == StreamingPlaybackState.Playing && !_fullyDownloaded)
                        {
                            _playbackState = StreamingPlaybackState.Buffering;
                            WavePlayer.Pause();
                        }
                        else if (bufferedSeconds >= 1 && _playbackState == StreamingPlaybackState.Buffering)
                        {
                            WavePlayer.Play();
                            _playbackState = StreamingPlaybackState.Playing;
                        }
                        else if (_fullyDownloaded && bufferedSeconds == 0)
                        {
                            Debug.WriteLine("Reached end of stream");
                            Stop();
                        }
                    }
                    UpdatePlayerState();
                }
            }
            finally
            {
                PositionChanging = false;
            }
        }
 public void Play()
 {
     if (playbackState == StreamingPlaybackState.Stopped)
     {
         playbackState = StreamingPlaybackState.Buffering;
         if (this.bufferedWaveProvider != null)
         {
             this.bufferedWaveProvider.ClearBuffer();
         }
         ThreadPool.QueueUserWorkItem(new WaitCallback(GetStreaming), null);
         timer1.Enabled = true;
     }
     else if (playbackState == StreamingPlaybackState.Paused)
     {
         playbackState = StreamingPlaybackState.Buffering;
     }
 }
Exemple #12
0
        public void Play(string uri)
        {
            _log.Verbose($"Start playing {uri}");
            _uri = uri;

            if (playbackState == StreamingPlaybackState.Stopped)
            {
                playbackState        = StreamingPlaybackState.Buffering;
                bufferedWaveProvider = null;
                ThreadPool.QueueUserWorkItem(StreamMp3, uri);
                _timer.Change(0, 1000);
            }
            else if (playbackState == StreamingPlaybackState.Paused)
            {
                playbackState = StreamingPlaybackState.Buffering;
            }
        }
Exemple #13
0
 public void Stop()
 {
     if (currentTrack != string.Empty)
     {
         currentTrack = string.Empty;
         CurrentTrack = null;
         if (OnNewTrack != null)
         {
             OnNewTrack(this, null);
         }
     }
     if (this.volumeProvider != null)
     {
         this.volumeProvider.Volume = 0.0f;
     }
     this.playbackState = StreamingPlaybackState.Stopped;
 }
Exemple #14
0
        public void Play(double pVolumeValue)
        {
            if (playbackState == StreamingPlaybackState.Stopped)
            {
                playbackState        = StreamingPlaybackState.Buffering;
                bufferedWaveProvider = null;

                _bufferedData = new BufferedData(65536);

                ThreadPool.QueueUserWorkItem(Mp3Running);
                ThreadPool.QueueUserWorkItem(PlayingMp3, pVolumeValue);
            }
            else if (playbackState == StreamingPlaybackState.Paused)
            {
                playbackState = StreamingPlaybackState.Buffering;
            }
        }
 private void BtnAudioparar1_Click(object sender, EventArgs e)
 {
     try
     {
         streamdois = false;
         if (estadodoplay1 == StreamingPlaybackState.Playing || estadodoplay1 == StreamingPlaybackState.Buffering)
         {
             saidadeaudiowave1.Pause();
             // Debug.WriteLine(String.Format("User requested Pause, waveOut.PlaybackState={0}", waveOut.PlaybackState));
             estadodoplay1 = StreamingPlaybackState.Paused;
         }
     }
     catch (Exception ex)
     {
         // Exibe exceção bruta de sistema caso não tenha mensagem personalizada
         ShowError(ex.Message + ex.StackTrace);
     }
 }
        private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (this.waveOut == null && this.bufferedWaveProvider != null)
                {
                    Logger.WriteDebug("Creating WaveOut Device");
                    this.waveOut               = CreateWaveOut();
                    waveOut.PlaybackStopped   += waveOut_PlaybackStopped;
                    this.volumeProvider        = new VolumeWaveProvider16(bufferedWaveProvider);
                    this.volumeProvider.Volume = this.volume;
                    waveOut.Init(volumeProvider);
                }
                if (bufferedWaveProvider != null)
                {
                    var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds;
                    // make it stutter less if we buffer up a decent amount before playing
                    if (bufferedSeconds < 0.5 && this.playbackState == StreamingPlaybackState.Playing && !this.fullyDownloaded)
                    {
                        this.playbackState = StreamingPlaybackState.Buffering;
                        waveOut.Pause();
                        Logger.WriteDebug(String.Format("Paused to buffer, waveOut.PlaybackState={0}", waveOut.PlaybackState));
                    }
                    else if (bufferedSeconds > secondsToBuffer && this.playbackState == StreamingPlaybackState.Buffering)
                    {
                        waveOut.Play();
                        Logger.WriteDebug(String.Format("Started playing, waveOut.PlaybackState={0}", waveOut.PlaybackState));
                        this.playbackState = StreamingPlaybackState.Playing;
                    }
                    // stop when right at the end
                    else if (this.fullyDownloaded && bufferedSeconds == 0)
                    {
                        Logger.WriteDebug("Reached end of stream");
                        Stop();

                        // handle end of track logic
                        if (OnEndOfTrack != null)
                        {
                            OnEndOfTrack();
                        }
                    }
                }
            }
        }
Exemple #17
0
        public void StopPlayback()
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (!fullyDownloaded)
                {
                }

                playbackState = StreamingPlaybackState.Stopped;
                if (waveOut != null)
                {
                    waveOut.Stop();
                    waveOut.Dispose();
                    waveOut = null;
                }

                // n、 可能尚未退出线程b
                Thread.Sleep(500);
            }
        }
 private void StopPlayback()
 {
     if (playbackState != StreamingPlaybackState.Stopped)
     {
         if (!fullyDownloaded)
         {
             webRequest.Abort();
         }
         this.playbackState = StreamingPlaybackState.Stopped;
         if (waveOut != null)
         {
             waveOut.Stop();
             waveOut.Dispose();
             waveOut = null;
         }
         timer.Change(Timeout.Infinite, Timeout.Infinite);
         // n.b. streaming thread may not yet have exited
         Thread.Sleep(500);
     }
 }
Exemple #19
0
        private void buttonPlay_Click(object sender, EventArgs e)
        {
            if (playbackState == StreamingPlaybackState.Stopped)
            {
                playbackState        = StreamingPlaybackState.Buffering;
                bufferedWaveProvider = null;
                Thread t = new Thread(new ThreadStart(StreamWorker))
                {
                    IsBackground = false
                };
                t.Start();
                ThreadPool.QueueUserWorkItem(StreamMp3, liveRadioURL);

                timer1.Enabled = true;
            }
            else if (playbackState == StreamingPlaybackState.Paused)
            {
                playbackState = StreamingPlaybackState.Buffering;
            }
        }
        private void InternalStopStream()
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (!fullyDownloaded)
                {
                    try
                    {
                        webRequest.Abort();
                    }
                    catch { }
                }

                playbackState = StreamingPlaybackState.Stopped;
                if (waveOut != null)
                {
                    try
                    {
                        waveOut.Stop();
                        waveOut.Dispose();
                    }
                    finally
                    {
                        waveOut = null;
                    }
                }
                if (processorWaveProvider != null)
                {
                    try
                    {
                        processorWaveProvider.Dispose();
                    }
                    finally
                    {
                        processorWaveProvider = null;
                    }
                }
                FirePropertyChangedAction(false);
                //SmartSleep(1500, "InternalStopStream");
            }
        }
Exemple #21
0
        private void StopPlayback()
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (!fullyDownloaded)
                {
                    webRequest.Abort();
                }

                playbackState = StreamingPlaybackState.Stopped;
                decomp        = false;
                if (audioSource != null)
                {
                    audioSource.Stop();
                    audioSource = null;
                }
                song_info = null;
                Thread.Sleep(500);
                ShowBufferState(0, 0);
            }
        }
 private void StopPlayback()
 {
     if (playbackState != StreamingPlaybackState.Stopped)
     {
         if (!fullyDownloaded)
         {
             webRequest.Abort();
         }
         this.playbackState = StreamingPlaybackState.Stopped;
         if (waveOut != null)
         {
             waveOut.Stop();
             waveOut.Dispose();
             waveOut = null;
         }
         timer1.Enabled = false;
         // n.b. streaming thread may not yet have exited
         Thread.Sleep(500);
         ShowBufferState(0);
     }
 }
Exemple #23
0
        private void stop_stream()
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (!fullyDownloaded)
                {
                    webRequest.Abort();
                }

                playbackState = StreamingPlaybackState.Stopped;
                if (waveOut != null)
                {
                    waveOut.Stop();
                    waveOut.Dispose();
                    waveOut = null;
                }
                timerStream.Enabled = false;
                Thread.Sleep(500);
                ShowBufferState(0);
            }
            sendStatus("Done");
        }
 private void timer1_Tick(object sender, EventArgs e)
 {
     if (playbackState != StreamingPlaybackState.Stopped)
     {
         if (this.waveOut == null && this.bufferedWaveProvider != null)
         {
             Debug.WriteLine("Creating WaveOut Device");
             this.waveOut               = CreateWaveOut();
             waveOut.PlaybackStopped   += waveOut_PlaybackStopped;
             this.volumeProvider        = new VolumeWaveProvider16(bufferedWaveProvider);
             this.volumeProvider.Volume = this.volumeSlider1.Volume;
             waveOut.Init(volumeProvider);
             progressBarBuffer.Maximum = (int)bufferedWaveProvider.BufferDuration.TotalMilliseconds;
         }
         else if (bufferedWaveProvider != null)
         {
             var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds;
             ShowBufferState(bufferedSeconds);
             // make it stutter less if we buffer up a decent amount before playing
             if (bufferedSeconds < 0.5 && this.playbackState == StreamingPlaybackState.Playing && !this.fullyDownloaded)
             {
                 this.playbackState = StreamingPlaybackState.Buffering;
                 waveOut.Pause();
                 Debug.WriteLine(String.Format("Paused to buffer, waveOut.PlaybackState={0}", waveOut.PlaybackState));
             }
             else if (bufferedSeconds > 4 && this.playbackState == StreamingPlaybackState.Buffering)
             {
                 waveOut.Play();
                 Debug.WriteLine(String.Format("Started playing, waveOut.PlaybackState={0}", waveOut.PlaybackState));
                 this.playbackState = StreamingPlaybackState.Playing;
             }
             else if (this.fullyDownloaded && bufferedSeconds == 0)
             {
                 Debug.WriteLine("Reached end of stream");
                 StopPlayback();
             }
         }
     }
 }
Exemple #25
0
        private async void buttonPlay_Click(object sender, RoutedEventArgs e)
        {
            if (playbackState == StreamingPlaybackState.Stopped)
            {
                playbackState        = StreamingPlaybackState.Buffering;
                bufferedWaveProvider = null;

                var stream = await _api.StreamTrackAsStream(new TrackStreamRequest()
                {
                    ReleaseId = Guid.Parse("09497970-9679-4ea6-930d-e1bf22cfc994"),
                    TrackId   = Guid.Parse("c8d3abc3-1668-42de-b832-b58ca6cc883f")
                });

                var task = Task.Run(() => Play(stream));
                _timer.Start();
                await task;
            }
            else if (playbackState == StreamingPlaybackState.Paused)
            {
                playbackState = StreamingPlaybackState.Buffering;
            }
        }
        private void PlayTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (TheLock)
            {
                if (m_PlaybackState != StreamingPlaybackState.Stopped)
                {
                    if (m_ASound != null)
                    {
                        if (m_ASound.WOut == null && m_ASound.WavProvider != null)
                        {
                            m_ASound.WOut                  = new WaveOut();
                            m_ASound.VolumeProvider        = new VolumeWaveProvider16(m_ASound.WavProvider);
                            m_ASound.VolumeProvider.Volume = 0.5f;
                            m_ASound.WOut.Init(m_ASound.VolumeProvider);
                        }
                        else if (m_ASound.WavProvider != null)
                        {
                            double BufferedSeconds = m_ASound.WavProvider.BufferedDuration.TotalSeconds;

                            //Make it stutter less if we buffer up a decent amount before playing
                            if (BufferedSeconds < 0.5 && m_PlaybackState == StreamingPlaybackState.Playing && !m_ASound.FullyStreamed)
                            {
                                m_PlaybackState = StreamingPlaybackState.Buffering;
                                m_ASound.WOut.Pause();
                            }
                            else if (BufferedSeconds > 4 && m_PlaybackState == StreamingPlaybackState.Buffering)
                            {
                                m_ASound.WOut.Play();
                                m_PlaybackState = StreamingPlaybackState.Playing;
                            }
                            else if (m_ASound.FullyStreamed && BufferedSeconds == 0)
                            {
                                StopPlayback();
                            }
                        }
                    }
                }
            }
        }
Exemple #27
0
        protected override void OnPositionChanged()
        {
            if (_reader != null)
            {
                lock (_repositionLocker)
                {
                    if (PositionChanging == false) // position changed with timer so _reader is up to date
                    {
                        if (_reader is PartialHttpStream)
                        {
                            _playbackState = StreamingPlaybackState.Buffering;
                        }

                        _bufferedWaveProvider.ClearBuffer();
                        // go to the begin of nearest frame
                        _reader.Position = Position + _dataStartPosition - (Position % Mp3WaveFormat.blockSize);
                        _fullyDownloaded = false;
                    }
                }
                CurrentTime = TimeSpan.FromSeconds((double)Position / Mp3WaveFormat.AverageBytesPerSecond);
                OnPropertyChanged(nameof(PositionPercent));
            }
        }
Exemple #28
0
        protected override void Stop()
        {
            if (_playbackState != StreamingPlaybackState.Stopped)
            {
                PlayerTimer.Stop(); // Note: stop timer before changing state
                Thread.Sleep(500);
                _playbackState = StreamingPlaybackState.Stopped;
                _reader?.Dispose();

                if (WavePlayer != null)
                {
                    WavePlayer.Stop();
                    WavePlayer.Dispose();
                    WavePlayer = null;
                }
                // n.b. streaming thread may not yet have exited
                Thread.Sleep(500);
                ShowBufferState(0);
            }
            UpdatePlayerState();
            Position = 0;
            SetTitle(Path.GetFileName(InputPath));
        }
        /// <summary>
        /// Creates a new SoundPlayer instance from a string,
        /// for playing MP3 music.
        /// </summary>
        /// <param name="MP3Sound">The name of an MP3 to play.</param>
        public SoundPlayer(string MP3Sound, bool LoopIt = true)
        {
            MP3File MP3 = (MP3File)FileManager.GetMusic(MP3Sound);

            /*DynamicSoundEffectInstance Instance = new DynamicSoundEffectInstance((int)MP3.GetSampleRate(), AudioChannels.Stereo);
             * Instance.BufferNeeded += Instance_BufferNeeded;*/

            m_ASound        = new ActiveSound();
            m_ASound.MP3    = MP3;
            m_ASound.LoopIt = LoopIt;
            //m_ASound.DynInstance = Instance;

            if (!FileManager.IsLinux)
            {
                m_ASound.PlayTimer          = new System.Timers.Timer();
                m_ASound.PlayTimer.Interval = 250;
                m_ASound.PlayTimer.Elapsed += PlayTimer_Elapsed;
                m_ASound.PlayTimer.Start();

                m_StreamingTask = new Task(new Action(ReadFromStream));
                m_PlaybackState = StreamingPlaybackState.Buffering;
                m_StreamingTask.Start();
            }
        }
Exemple #30
0
        private void StopPlayback()
        {
            Properties.Settings.Default.Volume = volumeProvider.Volume;
            Properties.Settings.Default.Save();

            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (!fullyDownloaded)
                {
                    webRequest.Abort();
                }

                playbackState = StreamingPlaybackState.Stopped;
                if (waveOut != null)
                {
                    waveOut.Stop();
                    waveOut.Dispose();
                    waveOut = null;
                }
                timer1.Enabled = false;
                Metarun        = false;
                Thread.Sleep(500);
            }
        }
        public void Stop()
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                stopwatch.Stop();
                stopwatch.Reset();
                if (!fullyDownloaded)
                {
                    webRequest.Abort();
                }

                playbackState = StreamingPlaybackState.Stopped;
                if (waveOut != null)
                {
                    waveOut.Stop();
                    waveOut.Dispose();
                    waveOut = null;
                }
                timer1.Enabled = false;
                Thread.Sleep(500); // n.b. streaming thread may not yet have exited
                SongLength = 0;
                UpdateTimer(stopwatch.Elapsed.TotalSeconds);
            }
        }
Exemple #32
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_playbackState != StreamingPlaybackState.Stopped)
            {
                if (_waveOut == null && _bufferedWaveProvider != null)
                {
                    Debug.WriteLine("Creating WaveOut Device");
                    _waveOut = CreateWaveOut();
                    _waveOut.PlaybackStopped += waveOut_PlaybackStopped;
                    _volumeProvider = new VolumeWaveProvider16(_bufferedWaveProvider);
                    _volumeProvider.Volume = 1;
                    _waveOut.Init(_volumeProvider);
                    progressBarBuffer.Properties.Maximum = (int)_bufferedWaveProvider.BufferDuration.TotalMilliseconds;
                }
                else if (_bufferedWaveProvider != null)
                {
                    var bufferedSeconds = _bufferedWaveProvider.BufferedDuration.TotalSeconds;
                    ShowBufferState(bufferedSeconds);
                    // make it stutter less if we buffer up a decent amount before playing
                    if (bufferedSeconds < 0.5 && _playbackState == StreamingPlaybackState.Playing && !_fullyDownloaded)
                    {
                        _playbackState = StreamingPlaybackState.Buffering;
                        _waveOut.Pause();
                        Debug.WriteLine(String.Format("Paused buffer, waveOut.PlaybackState={0}", _waveOut.PlaybackState));
                    }
                    else if (bufferedSeconds > 4 && _playbackState == StreamingPlaybackState.Buffering)
                    {
                        _waveOut.Play();
                        Debug.WriteLine(String.Format("Started playing, waveOut.PlaybackState={0}", _waveOut.PlaybackState));
                        _playbackState = StreamingPlaybackState.Playing;
                    }
                    else if (_fullyDownloaded && bufferedSeconds == 0)
                    {
                        Debug.WriteLine("End of stream");
                        StopPlayback();
                    }
                }

            }
        }
Exemple #33
0
        /** private void button2_Click(object sender, EventArgs e)
        {
            if (local)
            {
                PauseLocal();
            }
            else
            {
                PauseUrl();
            }

        }*/
        private void PauseUrl()
        {
            if (playbackState == StreamingPlaybackState.Playing || playbackState == StreamingPlaybackState.Buffering)
            {
                waveOut.Pause();
                Debug.WriteLine(String.Format("User requested Pause, waveOut.PlaybackState={0}", waveOut.PlaybackState));
                playbackState = StreamingPlaybackState.Paused;
            }
        }
        public void Stop()
        {
            try
            {
                _timer.Enabled = false;
                _timer.Dispose();
                _playbackState = StreamingPlaybackState.Stopped;

                if (_waveOut != null)
                {
                    _waveOut.Stop();
                    _waveOut.Dispose();
                    _waveOut = null;
                }

                if (_bufferThread != null && _bufferThread.IsAlive)
                {
                    _bufferThread.Join(1000);
                }
            }
            catch (Exception e)
            {
                _log.Log("Grooveshark: Error while stopping player. " + e.Message, Category.Info, Priority.Medium);
            }
            finally
            {
                _isPlaying(false);
                _elapsedTimeSpan = TimeSpan.Zero;
            }
        }
Exemple #35
0
 private void Play()
 {
     audioSource.Play();
     playbackState = StreamingPlaybackState.Playing;
 }
Exemple #36
0
        private void StopPlayback()
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (!fullyDownloaded)
                {
                    webRequest.Abort();
                }

                playbackState = StreamingPlaybackState.Stopped;
                if (waveOut != null)
                {
                    waveOut.Stop();
                    waveOut.Dispose();
                    waveOut = null;
                }
                timer1.Enabled = false;
                // n.b. streaming thread may not yet have exited
                Thread.Sleep(500);
                //ShowBufferState(0);
            }
        }
Exemple #37
0
 private void Play()
 {
     waveOut.Play();
     playbackState = StreamingPlaybackState.Playing;
 }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (this.waveOut == null && this.bufferedWaveProvider != null)
                {
                    Debug.WriteLine("Creating WaveOut Device");
                    this.waveOut = CreateWaveOut(); 
                    waveOut.PlaybackStopped += waveOut_PlaybackStopped;
                    this.volumeProvider = new VolumeWaveProvider16(bufferedWaveProvider);
                    this.volumeProvider.Volume = this.volumeSlider1.Volume;
                    waveOut.Init(volumeProvider);
                    progressBarBuffer.Maximum = (int)bufferedWaveProvider.BufferDuration.TotalMilliseconds;
                }
                else if (bufferedWaveProvider != null)
                {
                    var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds;
                    ShowBufferState(bufferedSeconds);
                    // make it stutter less if we buffer up a decent amount before playing
                    if (bufferedSeconds < 0.5 && this.playbackState == StreamingPlaybackState.Playing && !this.fullyDownloaded)
                    {
                        this.playbackState = StreamingPlaybackState.Buffering;
                        waveOut.Pause();
                        Debug.WriteLine(String.Format("Paused to buffer, waveOut.PlaybackState={0}", waveOut.PlaybackState));
                    }
                    else if (bufferedSeconds > 4 && this.playbackState == StreamingPlaybackState.Buffering)
                    {
                        waveOut.Play();
                        Debug.WriteLine(String.Format("Started playing, waveOut.PlaybackState={0}", waveOut.PlaybackState));
                        this.playbackState = StreamingPlaybackState.Playing;
                    }
                    else if (this.fullyDownloaded && bufferedSeconds == 0)
                    {
                        Debug.WriteLine("Reached end of stream");
                        StopPlayback();
                    }
                }

            }
        }
 private void Play()
 {
     waveOut.Play();
     Debug.WriteLine(String.Format("Started playing, waveOut.PlaybackState={0}", waveOut.PlaybackState));
     playbackState = StreamingPlaybackState.Playing;
 }
Exemple #40
0
        private void StopPlayback()
        {
            if (_playbackState != StreamingPlaybackState.Stopped)
            {
                if (!_fullyDownloaded)
                {
                    _webRequest.Abort();
                }
                _playbackState = StreamingPlaybackState.Stopped;
                if (_waveOut != null)
                {
                    _waveOut.Stop();
                    _waveOut.Dispose();
                    _waveOut = null;
                }
                timer1.Enabled = false;

                Thread.Sleep(500);
                ShowBufferState(0);
            }

            playButton.Visible = true;
            pauseButton.Visible = false;
            stopButton.Enabled = false;
        }
Exemple #41
0
        //------------------------------------------------------------------------------------------------------------------------

        public void Stop()
        {
            lock (this)
            {
                IsActive = false;
                playbackState = StreamingPlaybackState.Stopped;
            }
        }
Exemple #42
0
        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Provides a pcm stream that can be used to a waveout device (i.e. speakers) 
        /// or can write pcm samples to a System.IO.Stream
        /// </summary>
        public void Start()
        {
            //declares
            IMp3FrameDecompressor decompressor = null;
            HttpWebResponse resp = null;
            IsActive = true;

            //init state to buffering
            playbackState = StreamingPlaybackState.Buffering;

            //do web request
            var webRequest = (HttpWebRequest)WebRequest.Create(this.mp3url);
            try
            {
                resp = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (Exception ex)
            {
                DebugEx.Assert(ex);
            }
            if (resp == null)
                return;

            //local buffer that is overriden each  time a mp3 frame is decompressed
            var buffer = new byte[16384 * 4];
            var respStream = resp.GetResponseStream();
            var readFullyStream = new ReadFullyStream(respStream);

            //Streaming Loop
            try
            {
                while (playbackState != StreamingPlaybackState.Stopped)
                {
                    //get frame
                    Mp3Frame frame;
                    try
                    {
                        frame = Mp3Frame.LoadFromStream(readFullyStream);
                    }
                    catch (Exception ex)
                    {
                        DebugEx.TraceError(ex.Message);
                        break;
                    }

                    //get the codec info from the first frame
                    if (decompressor == null)
                    {
                        decompressor = Tools.CreateFrameDecompressor(frame);
                        audioFormat = new AudioFormat(decompressor.OutputFormat.SampleRate, (ushort)decompressor.OutputFormat.BitsPerSample, (ushort)decompressor.OutputFormat.Channels);
                        if (OnPCMStart != null)
                            OnPCMStart(this, decompressor.OutputFormat);
                    }

                    //write decompressed (pcm) samples to local buffer
                    int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
                    if (IsActive)
                    {
                        //fire event
                        if (OnAudioDataCaptured != null)
                            OnAudioDataCaptured(this, new AudioEventArgs(buffer, decompressed));
                    }
                }
            }
            catch (Exception ex)
            {
                DebugEx.TraceError(ex.Message);
            }
        }
 private void Pause()
 {
     playbackState = StreamingPlaybackState.Buffering;
     waveOut.Pause();
     Debug.WriteLine(String.Format("Paused to buffer, waveOut.PlaybackState={0}", waveOut.PlaybackState));
 }
        public void Play()
        {
            if (_playbackState == StreamingPlaybackState.Paused)
            {
                if (_waveOut != null)
                {
                    _waveOut.Play();
                    _isPlaying(true);
                    _playbackState = StreamingPlaybackState.Playing;
                }

                _timer.Enabled = true;
            }
        }
Exemple #45
0
        private void playButton_Click(object sender, EventArgs e)
        {
            if (_playbackState == StreamingPlaybackState.Stopped)
            {
                _playbackState = StreamingPlaybackState.Buffering;
                _bufferedWaveProvider = null;
                ThreadPool.QueueUserWorkItem(StreamMP3, _urlToSong);
                timer1.Enabled = true;
            }
            else if (_playbackState == StreamingPlaybackState.Paused)
            {
                _playbackState = StreamingPlaybackState.Buffering;
            }

            playButton.Visible = false;
            pauseButton.Visible = true;
            stopButton.Enabled = true;
        }
Exemple #46
0
 public void Stop()
 {
     if (playbackState != StreamingPlaybackState.Stopped)
     {
         playbackState = StreamingPlaybackState.Stopped;
         Disco.Bot.Client.SetGame(null);
     }
 }
        private void StreamMp3(object state)
        {
            _isBuffering(true);

            ThrottledStream responseStream = (ThrottledStream)state;
            byte[] buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame

            IMp3FrameDecompressor decompressor = null;
            try
            {
                using (responseStream)
                {
                    _playbackState = StreamingPlaybackState.Buffering;

                    using (var readFullyStream = new ReadFullyStream(responseStream))
                    {
                        do
                        {
                            if (_bufferedWaveProvider != null &&
                                _bufferedWaveProvider.BufferLength - _bufferedWaveProvider.BufferedBytes <
                                _bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4)
                            {
                                Thread.Sleep(500);
                            }

                            Mp3Frame frame;

                            try
                            {
                                frame = Mp3Frame.LoadFromStream(readFullyStream);

                                if (frame == null)
                                {
                                    _fullyDownloaded = true;
                                    break;
                                }
                            }
                            catch (EndOfStreamException e)
                            {
                                _log.Log(e.Message, Category.Warn, Priority.Medium);
                                _fullyDownloaded = true;
                                // reached the end of the MP3 file / stream
                                break;
                            }
                            catch (WebException e)
                            {
                                _log.Log(e.Message, Category.Warn, Priority.Medium);
                                _fullyDownloaded = true;
                                // probably we have aborted download from the GUI thread
                                break;
                            }
                            catch (Exception e)
                            {
                                _log.Log(e.Message, Category.Exception, Priority.High);
                                _fullyDownloaded = true;
                                break;
                            }

                            if (decompressor == null)
                            {
                                WaveFormat waveFormat = new Mp3WaveFormat(44100,
                                                                          frame.ChannelMode == ChannelMode.Mono
                                                                              ? 1
                                                                              : 2, frame.FrameLength, frame.BitRate);
                                decompressor = new AcmMp3FrameDecompressor(waveFormat);
                                _bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);

                                if (_track.TotalDuration != TimeSpan.Zero)
                                {
                                    _bufferedWaveProvider.BufferDuration = _track.TotalDuration;
                                }
                                else
                                {
                                    _bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(25);
                                }

                                responseStream.MaximumBytesPerSecond = _bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4;
                            }

                            if (_bufferedWaveProvider != null)
                            {
                                try
                                {
                                    int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
                                    _bufferedWaveProvider.AddSamples(buffer, 0, decompressed);
                                }
                                catch (Exception e)
                                {
                                    _fullyDownloaded = true;
                                    _log.Log("Grooveshark: Error decompressing frame: " + e.Message, Category.Exception, Priority.Medium);
                                    break;
                                }
                            }

                        } while (_playbackState != StreamingPlaybackState.Stopped);

                        // was doing this in a finally block, but for some reason
                        // we are hanging on response stream .Dispose so never get there
                        if (decompressor != null)
                        {
                            decompressor.Dispose();
                            decompressor = null;
                        }
                    }
                }
            }
            finally
            {
                if (decompressor != null)
                {
                    decompressor.Dispose();
                    decompressor = null;
                }
            }

            _log.Log("Grooveshark: Buffer thread exiting", Category.Info, Priority.Medium);
        }
Exemple #48
0
        private void StartPlay()
        {
            try
            {
                    var outFormat = new WaveFormat(48000, 16, 2);
                    using (var resampler = new MediaFoundationResampler(bufferedWaveProvider, outFormat))
                    {
                        resampler.ResamplerQuality = 60;
                        int blocksize = resampler.WaveFormat.AverageBytesPerSecond / 5;
                        byte[] buffer = new byte[blocksize];

                    VolumeWaveProvider16 vol = new VolumeWaveProvider16(resampler);
                    vol.Volume = 0.3f;

                    while (bufferedWaveProvider.BufferedBytes > bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 5 && playbackState != StreamingPlaybackState.Stopped)
                    {
                        vol.Read(buffer, 0, blocksize);
                        _client.Send(buffer, 0, blocksize);
                        _client.Wait();
                    }
                }
            }
            catch (Exception e2)
            {
                Console.Write(e2.ToString());
                return;
            }

            if(playbackState != StreamingPlaybackState.Stopped)
                playbackState = StreamingPlaybackState.Buffering;

            Console.WriteLine(String.Format("Gotta buffer"));          
        }
Exemple #49
0
 private void PlayUrl()
 {
     if (playbackState == StreamingPlaybackState.Stopped)
     {
         //string url = @"C:\Users\Kevin\Music\iTunes\iTunes Media\Music\Foo Fighters\Greatest Hits\14 Wheels";
         string url = "https://nyportalvhds1vwfglv3fyfp.blob.core.windows.net/odysseycloudmusiclibrary/Skyjelly_-_09_-_Sixes_Are_Silent_Alternate_Version%20(1).mp3";
         playbackState = StreamingPlaybackState.Buffering;
         bufferedWaveProvider = null;
         ThreadPool.QueueUserWorkItem(StreamMp3, source);
         timer1.Enabled = true;
     }
     else if (playbackState == StreamingPlaybackState.Paused)
     {
         playbackState = StreamingPlaybackState.Buffering;
     }
 }
Exemple #50
0
 public void Pause()
 {
     playbackState = StreamingPlaybackState.Buffering;
 }
Exemple #51
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (waveOut == null && bufferedWaveProvider != null)
                {
                    Debug.WriteLine("Creating WaveOut Device");
                    waveOut = CreateWaveOut();
                    //waveOut.PlaybackStopped += OnPlaybackStopped;
                    volumeProvider = new VolumeWaveProvider16(bufferedWaveProvider);
                    //volumeProvider.Volume = volumeSlider1.Volume;
                    waveOut.Init(volumeProvider);
                }
                else if (bufferedWaveProvider != null)
                {
                    var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds;
                    //ShowBufferState(bufferedSeconds);
                    // make it stutter less if we buffer up a decent amount before playing
                    if (bufferedSeconds < 0.5 && playbackState == StreamingPlaybackState.Playing && !fullyDownloaded)
                    {
                        Pause();
                    }
                    else if (bufferedSeconds > 4 && playbackState == StreamingPlaybackState.Buffering)
                    {
                        Play();
                    }
                    else if (fullyDownloaded && bufferedSeconds == 0)
                    {
                        Debug.WriteLine("Reached end of stream");
                        playbackState = StreamingPlaybackState.Stopped;
                        StopPlayback();
                    }
                }

            }
        }
Exemple #52
0
        private void pauseButton_Click(object sender, EventArgs e)
        {
            if (_playbackState == StreamingPlaybackState.Playing || _playbackState == StreamingPlaybackState.Buffering)
            {
                _waveOut.Pause();
                Debug.WriteLine(String.Format("User requested Pause, waveOut.PlaybackState={0}", _waveOut.PlaybackState));
                _playbackState = StreamingPlaybackState.Paused;
            }

            playButton.Visible = true;
            pauseButton.Visible = false;
            stopButton.Enabled = false;
        }
Exemple #53
0
 private void Pause()
 {
     playbackState = StreamingPlaybackState.Buffering;
     waveOut.Pause();
 }
Exemple #54
0
        private void Stream(IAudioClient client)
        {
            IMp3FrameDecompressor decompressor = null;

            ShoutcastStream shoutStream = new ShoutcastStream(server);
            shoutStream.StreamTitleChanged += ChangedTitle;
            var buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame

            try
            {
                    do
                    {

                        if (IsBufferNearlyFull)
                        {
                            Console.WriteLine("Buffer getting full, taking a break");
                            Thread.Sleep(500);
                        }
                        else
                        {
                            Mp3Frame frame;
                            try
                            {
                                frame = Mp3Frame.LoadFromStream(shoutStream);
                            }
                            catch (EndOfStreamException)
                            {
                                fullyDownloaded = true;
                                // reached the end of the MP3 file / stream
                                break;
                            }
                            catch (WebException)
                            {
                                // probably we have aborted download from the GUI thread
                                break;
                            }
                            if (decompressor == null)
                            {
                                // don't think these details matter too much - just help ACM select the right codec
                                // however, the buffered provider doesn't know what sample rate it is working at
                                // until we have a frame
                                decompressor = CreateFrameDecompressor(frame);
                                bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);
                                bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves
                            }
                            int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
                            bufferedWaveProvider.AddSamples(buffer, 0, decompressed);
                        }

                        if (bufferedWaveProvider != null)
                        {
                            var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds;

                            if (bufferedSeconds > 4 && playbackState == StreamingPlaybackState.Buffering)
                            {
                                playbackState = StreamingPlaybackState.Playing;
                                Task.Run(() => { StartPlay(); });
                            }
                            else if (fullyDownloaded && bufferedSeconds == 0)
                            {
                                Console.WriteLine("Reached end of stream");
                                Stop();
                            }
                        }
                    } while (playbackState != StreamingPlaybackState.Stopped);
            
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (decompressor != null)
                {
                    decompressor.Dispose();
                }
            }
        }
        private void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (_playbackState != StreamingPlaybackState.Stopped)
            {
                if (_waveOut == null && _bufferedWaveProvider != null)
                {
                    _log.Log("Grooveshark: Initializing", Category.Info, Priority.Medium);

                    try
                    {
                        _waveOut = new WaveOutEvent();
                        _volumeProvider = new VolumeWaveProvider16(_bufferedWaveProvider);
                        _volumeProvider.Volume = Volume;
                        _waveOut.Init(_volumeProvider);
                    }
                    catch (Exception ex)
                    {
                        _log.Log("Grooveshark: " + ex.ToString(), Category.Exception, Priority.High);
                        _elapsedTimeSpan = TimeSpan.Zero;
                        _playbackState = StreamingPlaybackState.Stopped;
                        _timer.Stop();
                        _isPlaying(false);
                        _trackComplete(_track);
                    }
                }
                else if (_bufferedWaveProvider != null)
                {
                    try
                    {
                        var bufferedSeconds = _bufferedWaveProvider.BufferedDuration.TotalSeconds;
                        // make it stutter less if we buffer up a decent amount before playing
                        if (bufferedSeconds < 0.5 && _playbackState == StreamingPlaybackState.Playing && !_fullyDownloaded)
                        {
                            _isBuffering(true);

                            _log.Log("Grooveshark: Buffering..", Category.Info, Priority.Medium);

                            _playbackState = StreamingPlaybackState.Buffering;

                            if (_waveOut != null)
                            {
                                _waveOut.Pause();
                                _isPlaying(false);
                            }
                        }
                        else if (bufferedSeconds > 4 && _playbackState == StreamingPlaybackState.Buffering)
                        {
                            _log.Log("Grooveshark: Buffering complete", Category.Info, Priority.Medium);

                            if (_waveOut != null)
                            {
                                _waveOut.Play();
                                _playbackState = StreamingPlaybackState.Playing;
                                _isPlaying(true);
                            }

                            _isBuffering(false);
                        }
                        else if (_fullyDownloaded && bufferedSeconds < 0.5)
                        {
                            _log.Log("Grooveshark: Buffer empty and the stream is fully downloaded. Complete..", Category.Info, Priority.Medium);
                            _elapsedTimeSpan = TimeSpan.Zero;
                            _isPlaying(false);
                            _playbackState = StreamingPlaybackState.Stopped;
                            _timer.Stop();
                            _trackComplete(_track);
                        }
                    }
                    catch (Exception exception)
                    {
                        _log.Log(exception.ToString(), Category.Exception, Priority.Medium);
                    }
                }

                if (_playbackState == StreamingPlaybackState.Playing)
                {
                    _elapsedTimeSpan = _elapsedTimeSpan.Add(TimeSpan.FromMilliseconds(_timer.Interval));
                    _trackProgress(_track.TotalDuration.TotalMilliseconds, _elapsedTimeSpan.TotalMilliseconds);
                }
            }
        }
        private void InternalStopStream()
        {
            if (playbackState != StreamingPlaybackState.Stopped)
            {
                if (!fullyDownloaded)
                {
                    try
                    {
                        webRequest.Abort();
                    }
                    catch { }
                }

                playbackState = StreamingPlaybackState.Stopped;
                if (waveOut != null)
                {
                    try
                    {
                        waveOut.Stop();
                        waveOut.Dispose();
                    }
                    finally
                    {
                        waveOut = null;
                    }
                }
                if (processorWaveProvider != null)
                {
                    try
                    {
                        processorWaveProvider.Dispose();
                    }
                    finally
                    {
                        processorWaveProvider = null;
                    }
                }
                FirePropertyChangedAction(false);
                //SmartSleep(1500, "InternalStopStream");
            }
        }
Exemple #57
0
 private void Pause()
 {
     playbackState = StreamingPlaybackState.Buffering;
     audioSource.Pause();
 }
        private void WaveStreamProc()
        {
            string icyStreamName = string.Empty;
            while (_streamShouldPlay)
            {
                ResetFeedInactivityTimeout();
                SetFeedActive(true, false);
                System.Threading.Thread.Sleep(new System.Random().Next(100, 1500));
                try
                {
                    Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkCyan, "Initiating connection to {0}", _streamName);
                    playbackState = StreamingPlaybackState.Buffering;
                    FirePropertyChangedAction(false);
                    bufferedWaveProvider = null;
                    fullyDownloaded = false;
                    webRequest = (HttpWebRequest)WebRequest.Create(Common.UrlHelper.GetCorrectedStreamURL(_streamURL));
                    //webRequest.Timeout = webRequest.Timeout * 20;
                    /*test - start*/
                    webRequest.Headers.Clear();
                    webRequest.Headers.Add("Icy-MetaData", "1");
                    SetupWebRequestSettings(webRequest);
                    /*test - end*/
                    HttpWebResponse resp;
                    try
                    {
                        resp = SmartGetWebResponse(webRequest);// (HttpWebResponse)webRequest.GetResponse();
                        string newUrl = string.Empty;
                        if (!ValidateResponse(resp, out newUrl, out icyStreamName))
                        {
                            if (!string.IsNullOrWhiteSpace(newUrl))
                            {
                                webRequest = (HttpWebRequest)WebRequest.Create(newUrl);
                                //webRequest.Timeout = webRequest.Timeout * 20;
                                /*test - start*/
                                webRequest.Headers.Clear();
                                webRequest.Headers.Add("Icy-MetaData", "1");
                                SetupWebRequestSettings(webRequest);
                                /*test - end*/
                                resp = SmartGetWebResponse(webRequest);// (HttpWebResponse)webRequest.GetResponse();
                                PrintHttpWebResponseHeader(resp, "WaveStreamProc.Redirected", out icyStreamName);
                            }
                        }
                    }
                    catch (WebException e)
                    {
                        if (e.Status == WebExceptionStatus.ProtocolError)
                        {
                            Common.DebugHelper.WriteExceptionToLog("WaveStreamProcessor.WebRequest.GetResponse", e, false, _streamURL);

                            try
                            {
                                System.Net.HttpWebResponse httpRes = e.Response as System.Net.HttpWebResponse;
                                if (httpRes != null && httpRes.StatusCode == HttpStatusCode.NotFound)
                                {
                                    SetFeedActive(false, false);
                                }
                            }
                            catch { }
                        }
                        if (e.Status != WebExceptionStatus.RequestCanceled)
                        {
                            //ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "WaveStreamProcessor Error: {0}", e.Message);
                            //Common.DebugHelper.WriteExceptionToLog("WaveStreamProcessor Error", e, false);
                            InternalStopStream();
                            ClearStreamTitle();

                            SmartSleep(30 * 1000, string.Format("WebRequest.GetResponse: {0}", e.Status));
                        }
                        continue;
                    }
                    var buffer = new byte[16348 * 4];
                    IMp3FrameDecompressor decompressor = null;
                    try
                    {
                        int metaInt = -1;
                        bool bIsIcy = IsIcecastStream(resp, out metaInt);
                        if (!bIsIcy)
                        {
                            metaInt = -1;
                        }
                        using (var responseStream = resp.GetResponseStream())
                        {
                            Stream readFullyStream = null;
                            if (!string.IsNullOrWhiteSpace(icyStreamName))
                            {
                                UpdateStreamTitle(icyStreamName, false);
                                FirePropertyChangedAction(true);
                            }
                            if (bIsIcy && metaInt > 0)
                            {
                                readFullyStream = new ReadFullyStream(new IcyStream(responseStream, metaInt, ProcessIcyMetadata));
                            }
                            else
                            {
                                readFullyStream = new ReadFullyStream(responseStream);
                            }
                            do
                            {
                                if (IsBufferNearlyFull)
                                {
                                    SmartSleep(500);
                                    //ConsoleHelper.ColorWriteLine(ConsoleColor.DarkCyan, "Buffer is getting full, taking a break, {0}sec...", bufferedWaveProvider.BufferedDuration.TotalSeconds);
                                    //ConsoleHelper.ColorWriteLine(ConsoleColor.DarkCyan, "  {0}", _streamURL);
                                }
                                else
                                {
                                    Mp3Frame frame;
                                    try
                                    {
                                        frame = Mp3Frame.LoadFromStream(readFullyStream);
                                    }
                                    catch (EndOfStreamException eose)
                                    {
                                        fullyDownloaded = true;
                                        SmartSleep(1500, "EndOfStreamException");
                                        if (playbackState != StreamingPlaybackState.Stopped)
                                        {
                                            Common.DebugHelper.WriteExceptionToLog("Mp3Frame.LoadFromStream", eose, false, _streamURL);
                                        }
                                        continue;
                                    }
                                    catch (WebException wex)
                                    {
                                        InternalStopStream();
                                        SmartSleep(3 * 60 * 1000, "WebException");
                                        if (playbackState != StreamingPlaybackState.Stopped)
                                        {
                                            Common.DebugHelper.WriteExceptionToLog("Mp3Frame.LoadFromStream", wex, false, _streamURL);
                                        }
                                        continue;
                                    }
                                    if (frame != null)
                                    {
                                        KickFeedInactivityTimeout();
                                        SetFeedActive(true, true);
                                        if (decompressor == null)
                                        {
                                            try
                                            {
                                                Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkGray, "Creating MP3 Decompressor for {0}...", _streamURL);
                                                decompressor = CreateFrameDecompressor(frame);
                                                bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);
                                                bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20);

                                                processorWaveProvider = new ProcessorWaveProvider(_streamName, bufferedWaveProvider, _sigDelegate, _propertyChangedAction, _recordingEnabled, _recordingType, _recordingKickTime, _noiseFloor, _customNoiseFloor, _removeNoise, _decodeMDC1200, _decodeGEStar, _decodeFleetSync, _decodeP25);

                                                //volumeProvider = new VolumeWaveProvider16(bufferedWaveProvider);
                                                volumeProvider = new VolumeWaveProvider16(processorWaveProvider);
                                                volumeProvider.Volume = _initialVolume;
                                                waveOut = CreateWaveOut();
                                                waveOut.Init(volumeProvider);
                                                FirePropertyChangedAction(false);
                                            }
                                            catch (Exception ex)
                                            {
                                                Common.ConsoleHelper.ColorWriteLine(ConsoleColor.Red, "Excpetion in stream {0}: {1}", _streamURL, ex.Message);
                                                Common.DebugHelper.WriteExceptionToLog("WaveStreamProcessor", ex, false, string.Format("Exception in stream {0}", _streamURL));
                                                InternalStopStream();
                                                SmartSleep(3 * 60 * 1000, "Exception:CreateFrameDecompressor");
                                                continue;
                                            }
                                        }
                                        int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
                                        bufferedWaveProvider.AddSamples(buffer, 0, decompressed);

                                        var bufferedSeconds = bufferedWaveProvider.BufferedDuration.TotalSeconds;
                                        if (bufferedSeconds < 0.5 && playbackState == StreamingPlaybackState.Playing && !fullyDownloaded)
                                        {
                                            playbackState = StreamingPlaybackState.Buffering;
                                            FirePropertyChangedAction(false);
                                            waveOut.Pause();
                                            Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkRed, "Stream Paused... Buffering... {0}", _streamURL);
                                        }
                                        else if (bufferedSeconds > 4 && playbackState == StreamingPlaybackState.Buffering)
                                        {
                                            waveOut.Play();
                                            playbackState = StreamingPlaybackState.Playing;
                                            FirePropertyChangedAction(false);
                                            Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkGreen, "Stream Playing... {0}", _streamURL);
                                        }
                                    }
                                    else
                                    {
                                        if(IsFeedInactive())
                                        {
                                            try
                                            {
                                                Common.ConsoleHelper.ColorWriteLine(ConsoleColor.DarkYellow, "Restarting {0} due to inactivity...", _streamName);
                                                InternalStopStream();
                                            }
                                            finally
                                            {
                                                LongSmartSleep(FEED_NOT_ACTIVE_SLEEP_SECS, "WaveStreamProc.IsFeedInactive");
                                            }
                                        }
                                    }
                                }
                            }
                            while (playbackState != StreamingPlaybackState.Stopped);
                            if (decompressor != null)
                            {
                                try
                                {
                                    decompressor.Dispose();
                                }
                                finally
                                {
                                    decompressor = null;
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (decompressor != null)
                        {
                            try
                            {
                                decompressor.Dispose();
                            }
                            finally
                            {
                                decompressor = null;
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
#if DEBUG
                    Common.DebugHelper.WriteExceptionToLog("WaveStreamProc", ex, false, "General Catch");
#endif
                    try
                    {
                        InternalStopStream();
                    }
                    finally
                    {
                        LongSmartSleep(15, "WaveStreamProc");
                    }
                }
            }
        }
 public void Pause()
 {
     if (_waveOut != null)
     {
         _waveOut.Pause();
         _isPlaying(false);
         _playbackState = StreamingPlaybackState.Paused;
     }
 }
 public WaveStreamProcessor(string streamURL, string streamName, Common.ProcessRadioSignalingItemDelegate sigDelegate, Action<bool> propertyChangedAction, float initialVolume, bool recordingEnabled, Common.SignalRecordingType recordingType, int recordingKickTime, Common.NoiseFloor noiseFloor, int customNoiseFloor, bool removeNoise, bool decodeMDC1200, bool decodeGEStar, bool decodeFleetSync, bool decodeP25, string waveOutDevName)
 {
     _streamShouldPlay = true;
     _streamURL = streamURL;
     _streamName = streamName;
     _sigDelegate = sigDelegate;
     _propertyChangedAction = propertyChangedAction;
     playbackState = StreamingPlaybackState.Stopped;
     _initialVolume = Math.Max(initialVolume, 0.1f);
     _recordingEnabled = recordingEnabled;
     _recordingType = recordingType;
     _recordingKickTime = recordingKickTime;
     _noiseFloor = noiseFloor;
     _customNoiseFloor = customNoiseFloor;
     _removeNoise = removeNoise;
     _decodeMDC1200 = decodeMDC1200;
     _decodeGEStar = decodeGEStar;
     _decodeFleetSync = decodeFleetSync;
     _decodeP25 = decodeP25;
     _waveOutDevName = waveOutDevName;
     System.Threading.Thread tWave = new Thread(WaveStreamProc);
     tWave.Name = streamName;
     tWave.IsBackground = true;
     tWave.Start();
 }