Exemplo n.º 1
0
        /// <summary>
        /// Event listener on sound change, sets complete boolean value
        /// to true to trigger save event
        /// </summary>
        /// <param name="url"></param>
        private void SongChange(string url)
        {
            var radio = new Radio(url);

            radio.OnCurrentSongChanged += (sender, eventArgs) =>
            {
                CurrentSongName = FromatSongName(eventArgs.NewSong.Artist, eventArgs.NewSong.Title);
                Complete        = true;
                Console.WriteLine(eventArgs.NewSong.Artist + " - " + eventArgs.NewSong.Title);

                if (CurrentStreamModel.SongChangeThreadToken.IsCancellationRequested)
                {
                    radio.Stop();
                }
            };

            radio.Start();
            GC.KeepAlive(radio);
        }
Exemplo n.º 2
0
        void GetHttpStream()
        {
            do
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                    request.Headers.Add("icy-metadata", "1");
                    request.ReadWriteTimeout = 10 * 1000;
                    request.Timeout          = 10 * 1000;
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        //get the position of metadata
                        int metaInt = 0;
                        if (!string.IsNullOrEmpty(response.GetResponseHeader("icy-metaint")))
                        {
                            metaInt = Convert.ToInt32(response.GetResponseHeader("icy-metaint"));
                        }
                        using (Stream socketStream = response.GetResponseStream())
                        {
                            byte[]        buffer         = new byte[16384];
                            int           metadataLength = 0;
                            int           streamPosition = 0;
                            int           bufferPosition = 0;
                            int           readBytes      = 0;
                            StringBuilder metadataSb     = new StringBuilder();

                            while (Running)
                            {
                                if (bufferPosition >= readBytes)
                                {
                                    readBytes      = socketStream.Read(buffer, 0, buffer.Length);
                                    bufferPosition = 0;
                                }
                                if (readBytes <= 0)
                                {
                                    Radio.Log("Stream over", this);
                                    break;
                                }

                                if (metadataLength == 0)
                                {
                                    if (metaInt == 0 || streamPosition + readBytes - bufferPosition <= metaInt)
                                    {
                                        streamPosition += readBytes - bufferPosition;
                                        ProcessStreamData(buffer, ref bufferPosition, readBytes - bufferPosition);
                                        continue;
                                    }

                                    ProcessStreamData(buffer, ref bufferPosition, metaInt - streamPosition);
                                    metadataLength = Convert.ToInt32(buffer[bufferPosition++]) * 16;
                                    //check if there's any metadata, otherwise skip to next block
                                    if (metadataLength == 0)
                                    {
                                        streamPosition = Math.Min(readBytes - bufferPosition, metaInt);
                                        ProcessStreamData(buffer, ref bufferPosition, streamPosition);
                                        continue;
                                    }
                                }

                                //get the metadata and reset the position
                                while (bufferPosition < readBytes)
                                {
                                    metadataSb.Append(Convert.ToChar(buffer[bufferPosition++]));
                                    metadataLength--;
                                    if (metadataLength == 0)
                                    {
                                        Metadata = metadataSb.ToString();
                                        metadataSb.Clear();
                                        streamPosition = Math.Min(readBytes - bufferPosition, metaInt);
                                        ProcessStreamData(buffer, ref bufferPosition, streamPosition);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                    Radio.Log(string.Format("Handled IOException, reconnecting. Details:\n{0}\n{1}", ex.Message, ex.StackTrace), this);
                    OnStreamOver?.Invoke(this, new StreamOverEventArgs());
                }
                catch (SocketException ex)
                {
                    Radio.Log(string.Format("Handled SocketException, reconnecting. Details:\n{0}\n{1}", ex.Message, ex.StackTrace), this);
                    OnStreamOver?.Invoke(this, new StreamOverEventArgs());
                }
                catch (WebException ex)
                {
                    Radio.Log(string.Format("Handled WebException, reconnecting. Details:\n{0}\n{1}", ex.Message, ex.StackTrace), this);
                    OnStreamOver?.Invoke(this, new StreamOverEventArgs());
                }
            } while (Running);
        }