private void _onPlaybackErrorChanged_SignalRaised(ISignal sender, IList <object> args) { OnPlaybackError?.Invoke(this, new PlaybackErrorEventArgs(args)); }
private static void StreamMp3(object state) { FullyDownloaded = false; var url = (Uri)state; _webRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp; try { resp = (HttpWebResponse)_webRequest.GetResponse(); } catch (WebException e) { OnPlaybackError?.Invoke(_webRequest, e); return; } var buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame IMp3FrameDecompressor decompressor = null; try { using (var responseStream = resp.GetResponseStream()) { var readFullyStream = new ReadFullyStream(responseStream); do { if (IsBufferNearlyFull) { //Debug.WriteLine("Buffer getting full, taking a break"); Thread.Sleep(500); continue; } Mp3Frame frame; try { frame = Mp3Frame.LoadFromStream(readFullyStream); } 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) { BufferDuration = TimeSpan.FromSeconds(60) }; // allow us to get well ahead of ourselves //this._bufferedWaveProvider.BufferedDuration = 250; } if (frame == null) { FullyDownloaded = true; break; } int decompressed = decompressor.DecompressFrame(frame, buffer, 0); //Debug.WriteLine(String.Format("Decompressed a frame {0}", decompressed)); _bufferedWaveProvider.AddSamples(buffer, 0, decompressed); } 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 decompressor?.Dispose(); } } finally { decompressor?.Dispose(); } }