1

I currently making simple music player and would like to stream online radio. I managed to stream ShoutCast radio but the problem is I have no idea how to parse the title and artist from streaming metadata. Here is my code.

Player.cs

    public string[] GetTags(bool streaming)
    {
        if (streaming == true)
        {
            IntPtr tag = Bass.BASS_ChannelGetTags(stream, BASSTag.BASS_TAG_META);
            string[] tags = Utils.IntPtrToArrayNullTermUtf8(tag);
            if (tags != null)
            {
                return tags;
            }            
        }
        return null;
    }

Main.cs

  private void btnLoadURL_Click(object sender, EventArgs e)
    {
        p.LoadURL(tbFile.Text);
        string[] tags = p.GetTags(true);
        if (tags != null) 
        {
            foreach (String tag in tags)
            {
                lblStatus.Text = tag;
            }
        }
    }

Currently I need to iterate through the tags to get the metadata in the format StreamTitle='xxx';StreamUrl='xxx';. I would like to parse this into;

Title: xxx

Artist: xxx

and remove StreamUrl entirely.

Thanks!

Zerocchi
  • 614
  • 1
  • 8
  • 20

1 Answers1

1

My own approach is to concatenate an array of strings into a string using String.Join method

string conTitle = String.Join("", tags);

then by using regular expression I am able to extract artist and song from the string:

if (tags != null)
            {
                string ConTitle = String.Join("", tags);
                string FullTitle = Regex.Match(ConTitle,
                  "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();
                string[] Title = Regex.Split(FullTitle, " - ");
                return Title;
            }          

In Main.cs I iterate the returned value and assign the variable according to the string[] index

if (tags != null) 
        {
            foreach (string tag in tags)
            {
                lblArtist.Text = tags[0];
                lblTitle.Text = tags[1];
            }
        }

Here's the player image since I don't have enough rep yet to upload one I have enough rep now, so here's the image.

Though I have to look into the regex back since album title also appeared there.

EDIT: Here's the modified regex:

Regex.Match(ConTitle, "(StreamTitle=')(.*)(\\(.*\\)';StreamUrl)").Groups[2].Value.Trim();

Now no more bracket with album title after song title.

Zerocchi
  • 614
  • 1
  • 8
  • 20
  • Just beware that the `StreamTitle` value isn't always in the format of `Artist - Title`. This is just a convention often followed, but often not. I would guess that it works around 70% of the time, but not higher. – Brad Sep 04 '14 at 12:10
  • Yeah you are right, guess I should find a way to parse them regardless of format they are on. Thanks for the heads up! – Zerocchi Sep 05 '14 at 09:40