示例#1
0
        //Function that reads in infromation from the stream and formats the metadata to display in the application
        public async void GetMetaDataStream()
        {
            try
            {
                client.DefaultRequestHeaders.Add("Icy-MetaData", "1");
                response = await client.GetAsync(selected, HttpCompletionOption.ResponseHeadersRead);

                client.DefaultRequestHeaders.Remove("Icy-MetaData");
            }
            catch (HttpRequestException e)
            {
                playButton.Image = Properties.Resources.if_button_grey_play_50039;
                playButton.Refresh();
                playButton.Update();
                playButton.Invalidate();
                mp.controls.pause();
                IsPlaying  = false;
                errorState = 1;
                timer.Stop();
                timer2.Start();
                return;
            }
            if (response.IsSuccessStatusCode)
            {
                IEnumerable <string> headerValues;
                if (response.Headers.TryGetValues("icy-metaint", out headerValues))
                {
                    string metaIntString = headerValues.First();
                    if (!string.IsNullOrEmpty(metaIntString))
                    {
                        int    metadataInterval = int.Parse(metaIntString);
                        byte[] buffer           = new byte[metadataInterval + 10];
                        using (var stream = await response.Content.ReadAsStreamAsync())
                        {
                            int numBytesRead   = 0;
                            int numBytesToRead = metadataInterval;
                            do
                            {
                                try
                                {
                                    n = stream.Read(buffer, numBytesRead, 10);
                                }catch
                                {
                                    errorState = 1;
                                    timer.Stop();
                                    return;
                                }
                                numBytesRead   += n;
                                numBytesToRead -= n;
                            } while (numBytesToRead > 0);

                            int    lengthOfMetaData = stream.ReadByte();
                            int    metaBytesToRead  = lengthOfMetaData * 16;
                            byte[] metadataBytes    = new byte[metaBytesToRead];
                            try
                            {
                                var bytesRead = await stream.ReadAsync(metadataBytes, 0, metaBytesToRead);
                            }catch
                            {
                                errorState = 1;
                                timer.Stop();
                                return;
                            }

                            metaDataString = System.Text.Encoding.UTF8.GetString(metadataBytes);
                            metaString     = metaDataString;
                            chars1         = new ArrayList();
                            String s;

                            foreach (var c in metaString)
                            {
                                if (!char.IsControl(c))
                                {
                                    chars1.Add(c);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            char[] chars2 = (char[])chars1.ToArray(typeof(char));
                            s = new String(chars2);
                            if (s.Contains("-"))
                            {
                                title  = Regex.Split(s, "-");
                                stuff  = Regex.Split(title[0], "'");
                                Artist = stuff[1];
                                if (title.Length > 2)
                                {
                                    title[1] = title[1] + " - " + title[2];
                                }
                                if (s.Contains(";") && s.Contains("'"))
                                {
                                    if (title[1].Length > 4)
                                    {
                                        title[1] = title[1].Substring(1, title[1].Length - 2);
                                        if (title[1].LastIndexOf("'") == title[1].Length - 1)
                                        {
                                            title[1] = title[1].Remove(title[1].LastIndexOf("'"));
                                        }
                                        ArtistLabel.Text = Artist;
                                        TitleLabel.Text  = title[1];
                                    }
                                    else
                                    {
                                        ArtistLabel.Text = Artist;
                                        TitleLabel.Text  = title[1];
                                    }
                                }

                                ArtistLabel.Text = Artist;
                                TitleLabel.Text  = title[1];
                            }
                            else
                            {
                                ArtistLabel.Text = "Information Unavailable";
                                TitleLabel.Text  = "Information Unavailable";
                            }

                            ArtistLabel.Refresh();
                            ArtistLabel.Update();
                            ArtistLabel.Invalidate();
                            TitleLabel.Refresh();
                            TitleLabel.Update();
                            TitleLabel.Invalidate();
                        }
                    }
                }
            }
        }