Exemplo n.º 1
0
        // modified from NAudio sample code but working with incoming bytes
        private void GetStreaming(object state)
        {
            this.fullyDownloaded = false;

            try
            {
                do
                {
                    if (bufferedWaveProvider == null)
                    {
                        this.bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(44100, 2));
                        this.bufferedWaveProvider.BufferDuration = TimeSpan.FromMinutes(20); // allow us to get well ahead of ourselves
                        Logger.WriteDebug("Creating buffered wave provider");
                    }
                    // in case its still null
                    if (bufferedWaveProvider == null)
                    {
                        continue;
                    }

                    var freeBufferBytes = bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes;
                    if (freeBufferBytes < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4)
                    {
                        Logger.WriteDebug("Buffer getting full, taking a break");
                        Thread.Sleep(450);
                    }
                    Thread.Sleep(250);
                } while (playbackState != StreamingPlaybackState.Stopped);
                Logger.WriteDebug("Playback stopped");
            }
            finally
            {
                // no post-processing work here, right?
            }
        }
Exemplo n.º 2
0
 private void waveOut_PlaybackStopped(object sender, StoppedEventArgs e)
 {
     Logger.WriteDebug("Playback Stopped");
     if (e.Exception != null && OnError != null)
     {
         OnError(e.Exception.Message);
     }
 }
Exemplo n.º 3
0
 public void Pause()
 {
     if (playbackState == StreamingPlaybackState.Playing || playbackState == StreamingPlaybackState.Buffering)
     {
         waveOut.Pause();
         Logger.WriteDebug(String.Format("User requested Pause, waveOut.PlaybackState={0}", waveOut.PlaybackState));
         playbackState = StreamingPlaybackState.Paused;
     }
 }
Exemplo n.º 4
0
        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();
                        }
                    }
                }
            }
        }