예제 #1
0
        public void Play()
        {
            BasePlayer.Playing = true;
            playing            = true;

            provideThread = new Thread(() =>
            {
                try
                {
                    WebClient wc = new WebClient();
                    wc.Headers[HttpRequestHeader.UserAgent] = Globals.USER_AGENT;

                    BaseStream = wc.OpenRead(url);

                    var readFullyStream = new ReadFullyStream(BaseStream);

                    var pc = 0;

                    while (playing)
                    {
                        byte[][] packets;
                        try
                        {
                            packets = ogg.GetAudioPackets(readFullyStream);
                        }
                        catch (System.IO.IOException) { break; }

                        if (++pc <= 5)
                        {
                            continue;
                        }

                        for (int i = 0; i < packets.Length; i++)
                        {
                            var streamBytes = packets[i];
                            try
                            {
                                int frameSize     = OpusPacketInfo.GetNumSamplesPerFrame(streamBytes, 0, Globals.SAMPLE_RATE); //Get frame size from opus packet
                                short[] rawBuffer = new short[frameSize * 2];                                                  //2 channels
                                var buffer        = decoder.Decode(streamBytes, 0, streamBytes.Length, rawBuffer, 0, frameSize, false);
                                BasePlayer.QueueBuffer(rawBuffer);

                                if (visualiser != null)
                                {
                                    visualiser.AddSamples(rawBuffer);
                                }
                            }
                            catch (Concentus.OpusException)
                            {
                                //Skip this frame
                            }
                        }
                    }
                }
                catch (WebException)
                {
                    Thread.Sleep(250); Play();
                }
                catch (Exception)
                {
                }
            });
            provideThread.Start();
        }