开发者

Read Song Title/Artist from a live audio stream with Silverlight 4?

I have a SL4 project that is successfully streaming a great sounding WMA audio stream from a remote location. All of the MediaElement actions are straight forward.

What I want to do is read the attributes that are passed as text along with the Audio stream. For instance the encoder of the stream embeds the title of the stream, the title of the song playing and the name of the artist for the current song.

How would I pick this out using Silverlight 4 and then display it in a Label to the user?

It sure would be easier than writing a bunch of web services to do the same thing. Windows Media Player and WinAmp all get the information I am just not seeing it in 开发者_Go百科the MediaElement object collection.


I found the answer after searchting the web as well as fiddling with Expression 3 a little as well.

It turns out that a live audio stream has markers that are sent across as well as the audio. Markers can contain almost anything but one is called a "Caption". The caption is basically a free-form string field that you can read. With my stream the encoder sends a lot of information across as a caption that can then be broken down. So here is the code I am using:

Starts with registering a few events, the last one is the important one.

public MainPage()
    {
        InitializeComponent();
        this.mediaElement1.BufferingProgressChanged += new RoutedEventHandler(mediaElement1_BufferingProgressChanged);
        this.mediaElement1.MarkerReached += new TimelineMarkerRoutedEventHandler(mediaElement1_MarkerReached);
    }

Then the actual marker handler does the following:

private void mediaElement1_MarkerReached(object sender, TimelineMarkerRoutedEventArgs e)
    {
        Dictionary<string, string> songAttribs = new Dictionary<string, string>();
        string playerFeed = HttpUtility.UrlDecode(e.Marker.Text);
        char[] delims = { '&' };
        string[] Attribs = playerFeed.Split(delims);

        foreach (String attrib in Attribs)
        {
            string[] keypair = attrib.Split('=');
            string key = "";
            string value = "";

            try
            {
                key = keypair[0];
            }
            catch
            {
                key = null;
            }

            if (key != null)
            {
                try
                {
                    value = keypair[1];
                }
                catch
                {
                    value = "";
                }

                songAttribs.Add(keypair[0], keypair[1]);
            }
        }

        nowplaying.Title = songAttribs["title"];
        nowplaying.Artist = songAttribs["artist"];
        nowplaying.Duration = 0;

        this.label2.Content = "Artist: " + nowplaying.Artist;
        this.label3.Content = "Title: " + nowplaying.Title;

        this.label1.Content = playerFeed;
    }

Still working on some of the code but so far things seem to be working.


Mayba WMP and WinAmp get the Informations from a Website or something like this, and dont read it out of the stream...

Werewolve

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜