2014-09-04 55 views
1

我目前正在制作简单的音乐播放器并希望流式播放在线广播。我设法传播ShoutCast广播,但问题是我不知道如何从流媒体元数据解析标题和艺术家。这是我的代码。获取/解析ShoutCast元数据

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; 
      } 
     } 
    } 

目前我需要通过tags迭代,以获得元数据格式StreamTitle='xxx';StreamUrl='xxx';。我想解析一下;

名称:XXX

艺术家:XXX

和除去StreamUrl完全。

谢谢!

回答

1

我自己的方法是使用正则表达式我能够从字符串中提取艺术家和歌曲使用String.Join方法

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

的字符串连接成一个字符串数组,然后:

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我迭代返回值并根据字符串[]分配变量索引

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.

虽然我要看看正则表达式后面,因为专辑名称也出现在那里。

编辑:这里的修改后的正则表达式:

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

现在没有更多的支架与歌名后,专辑名称。

+0

只要注意'StreamTitle'的值并不总是'Artist - Title'的格式。这只是一个常常遵循的惯例,但往往不是。我猜测它可以在70%左右的时间内工作,但不会更高。 – Brad 2014-09-04 12:10:41

+0

是的你是对的,我想我应该找到一种方法来解析它们,不管它们的格式如何。感谢您的领导! – Zerocchi 2014-09-05 09:40:53