Exemplo n.º 1
0
        private void ButtonBase2_OnClick(object sender, RoutedEventArgs e)
        {
            var msStream        = WebApiProxy.GetFile(@"http://win.web.nf01.sycdn.kuwo.cn/resource/n1/10/47/2323037830.mp3");
            var readFullyStream = new Framework.Recorder.ReadFullyStream(msStream);

            try
            {
                //var inputStream = new AudioFileReader(msStream);
                //var aggregator = new SampleAggregator(inputStream);

                //var waveOut = CreateWaveOut();
                ////waveOut.PlaybackStopped += OnPlaybackStopped;
                ////var volumeProvider = new VolumeWaveProvider16(bufferedWaveProvider);
                ////volumeProvider.Volume = volumeSlider1.Volume;
                //waveOut.Init(aggregator);
                //waveOut.Play();
            }
            catch (EndOfStreamException)
            {
                // reached the end of the MP3 file / stream
            }
            catch (WebException)
            {
                // probably we have aborted download from the GUI thread
            }
            catch (Exception)
            {
                // probably we have aborted download from the GUI thread
            }

            if (IsBufferNearlyFull)
            {
            }
        }
Exemplo n.º 2
0
        private void StreamMp3(object state)
        {
            fullyDownloaded = false;
            var url = (string)state;

            webRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse resp;

            try
            {
                resp = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException e)
            {
                if (e.Status != WebExceptionStatus.RequestCanceled)
                {
                    Log4NetHelper.Error(e.Message);
                }
                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)
                        {
                            Log4NetHelper.Error("Buffer getting full, taking a break");
                            Thread.Sleep(500);
                        }
                        else
                        {
                            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 (frame == null)
                            {
                                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
                                //this.bufferedWaveProvider.BufferedDuration = 250;
                            }
                            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);
                    Console.WriteLine("Exiting");
                    // 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
            {
                if (decompressor != null)
                {
                    decompressor.Dispose();
                }
            }
        }