Jump to content

Detect Streaming Music Feed And Change a Prim Color


Ghostwitness
 Share

You are about to reply to a thread that has been inactive for 3689 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I'm trying to do something really simple. I want to check to see if my music feed is active and if it is, set the prim color to white. If the feed is not active, set it to black. This would need to check every say 5 minutes or something. What I'm doing is a belt buckle with an ON AIR sign on it that lights up when I am on. I borrowed this code from another tread and thought I understood it enough, but it doesn't see to do anything when the feed is off. Perhaps this script is overkill, I dunno, not a big scripter.

Any help would be appreciated :)

 

// leave music_url blank if set in object's description
string music_url = "http://66.7.214.7:8064"; 

key HTTPRequest;
string feed;
string URL;
string currSongTitle;
string lastSongTitle;
list feedList;

default
{
    state_entry()
    {
        llSetText(" ",<1,1,1>,1);
        llSetTimerEvent(5.0);
    
        if(music_url) 
        {
            URL = music_url;
        } else {
            URL = llGetObjectDesc();
        }
    }

    timer()
    {
        HTTPRequest=llHTTPRequest(URL + "/7.html HTTP/1.0\nUser-Agent: LSL Script (Mozilla Compatible)\n\n",[],"");
    }

    http_response(key k,integer status, list meta, string body)
    {
        if(k != HTTPRequest) 
        {
            llSetColor(<0.067, 0.067, 0.067>, ALL_SIDES); 
            return;
        }

        feed = llGetSubString(body,llSubStringIndex(body, "<body>") + llStringLength("<body>"), llSubStringIndex(body,"</body>") - 1);
        feedList = llParseString2List(feed,[","],[]);
        currSongTitle = llList2String(feedList,6);
        integer length = llGetListLength(feedList);
        
        if(llList2String(feedList,7))
        {
            integer a = 7;
            for(; a<length; ++a)
            {
                currSongTitle += ", " + llList2String(feedList,a);
            }
        }
         
        if (currSongTitle != lastSongTitle)
        {
            llSetText(currSongTitle,<1,1,1>,0.75);
            llSetColor(<1.000, 1.000, 1.000>, ALL_SIDES); 
            lastSongTitle = currSongTitle;
        }
    }
}

 

Link to comment
Share on other sites


Ghostwitness wrote:
        HTTPRequest=llHTTPRequest(URL + "/7.html HTTP/1.0\nUser-Agent: LSL Script (Mozilla Compatible)\n\n",[],"");

 

The llHTTPRequest line is checking for a specific type of media stream. Ensure this destination is compatible with the type of media server you are streaming from.

The URI to check (according to your code) is:

http://66.7.214.7/7.html
Link to comment
Share on other sites

The above script is working when a song is being played, and it switches when a new song comes on. However, if I turn off the stream, it does not blank the hover text and set the color to black. I believe I am misunderstanding the code somewhere.

 

ONAIR.jpg

Link to comment
Share on other sites

Thanks. Considering that the llHTTPRequest resolves as you expect, the next portion to adjust would be:-


Ghostwitness wrote:

 
    http_response(key k,integer status, list meta, string body)    {        if(status != 200)     //FIXED KEY TEST, AS POST ABOVE FROM Nova Convair        {            llSetColor(<0.067, 0.067, 0.067>, ALL_SIDES);             //REMOVED 'return;' AS THIS BECOMES IMPLICIT        }        else //LINE ADDED
{ //LINE ADDED feed = llGetSubString(body,llSubStringIndex(body, "<body>") + llStringLength("<body>"), llSubStringIndex(body,"</body>") - 1); feedList = llParseString2List(feed,[","],[]); currSongTitle = llList2String(feedList,6); integer length = llGetListLength(feedList); if(llList2String(feedList,7)) { integer a = 7; for(; a<length; ++a) { currSongTitle += ", " + llList2String(feedList,a); } } if (currSongTitle != lastSongTitle) { llSetText(currSongTitle,<1,1,1>,0.75); llSetColor(<1.000, 1.000, 1.000>, ALL_SIDES); lastSongTitle = currSongTitle; }
} //LINE ADDED }}

 

  • status == 200 implies the page can be found. status != 200 is 'not found' or 'other error', this matches with HTTP Status Codes. Some LSL coders prefer the test
    if(status != 200 || status != 0)

     

     or
    if(status > 200 && status < 300)

     

  • I swapped your if to an if-else, because you don't need to react beyond the status check if the server is offline.
Link to comment
Share on other sites

You may need to debug this.

Add

llOwnerSay(llGetTimestamp() + ": " + (string)status);

 to the top line of your http_response event. The same event I pasted in my second response.

This will repeat to you, privately, the status of the llHTTPRequests as they respond.

When you 'turn off' the stream, does this status change? If not, I recommend pasting the chat-log of the scripts output here (it will include its own timestamp) - annotated to add when the stream is being turned off.

Link to comment
Share on other sites

Looks like we'll have to do this the hard way, in that case.

We can't test against status, so we should test against body instead.

Change the llOwnerSay you added, to this:

llOwnerSay(llGetTimestamp() + ": " + llGetSubString(body,0,64));

 This will instead output the first 64 characters of the destination (again privately, via local chat). This will be useful, as I can't see the outputs of the system.

Hopefully this output differs, depending on whether the server is online.

Could you post one line of its output while the stream is ON, and another line of its output while the stream is OFF? Feel free to 'XXXXX' out any identifying information about the server.

Link to comment
Share on other sites

When off --> Animated Belt Buckle: 2014-03-07T15:35:53.992924Z: <HTML><meta http-equiv="Pragma" content="no-cache"></head><body>0

When on with no song playing -->  Animated Belt Buckle: 2014-03-07T15:37:13.985207Z: <HTML><meta http-equiv="Pragma" content="no-cache"></head><body>0

When on with a song playing --> Animated Belt Buckle: 2014-03-07T15:38:29.001873Z: <HTML><meta http-equiv="Pragma" content="no-cache"></head><body>0

 

Seems to be the same no matter what. Yet song still displays in floating text while playing and updates when new song comes on.

 

Link to comment
Share on other sites

Aha, it generates HTML tags. My bad.

Use:-

llOwnerSay(llGetTimestamp() + ": " + llGetSubString(body,63,-1));

 and try again.

The reason I'm using llGetSubString rather than just getting the whole of body is to try and avoid making it too complicated to look at. Ideally, we only need to look at the bits inside of the HTML tags.

Link to comment
Share on other sites

When stream is turned off --> Animated Belt Buckle: 2014-03-07T15:45:43.312322Z: >0,0,22,100,0,128,blink-182 - First Date</body></html>

- Seems to be displaying last song played

When stream is on with no song --> Animated Belt Buckle: 2014-03-07T15:46:53.327011Z: >0,1,22,100,0,128,blink-182 - First Date</body></html>

- Still appears to display the last song played

When streaming with a song on -->  Animated Belt Buckle: 2014-03-07T15:48:18.314447Z: >0,1,22,100,0,128,Bob Seger - Her Strut</body></html>

- Displays current song

Link to comment
Share on other sites

You're correct, but we've found the important part:-


Ghostwitness wrote:

When stream is turned off --> 0,
0
,22,100,0,128,blink-182 - First Date

When stream is on --> 0,
1
,22,100,0,128,blink-182 - First Date

This second value (bolded) changes depending on whether the stream is on or off, and will be our test.

To test this works, try narrowing the scope of the llOwnerSay. This should work, but you might want to move it around a little* - ideally you want it to output either '1' or '0' depending on the running state of the stream.

 

llOwnerSay(llGetSubString(body,63 + 2,63 + 3));

This code has offsets above the (it's quickest to assume they're fixed) length of the HTML tags. You can update your llOwnerSay to this.

THe first value is the 'start position', the second is the 'end position'. The end position should be (start position + length of test string) - in this case '1' and '0' are both 1 character long, so it's only 1 number above the start position (63 + 2). 

*To move it around, increase or decrease the '+ 2' in my code above, while keeping the '+ 3' always 1 number larger (2 == 3, 3 == 4, etc) than the '+ 2'.

Link to comment
Share on other sites

Oh, whoops. Sorry, little difficult to do this remotely, and other progamming languages differ (which confuses me).

Use:-

llOwnerSay(llGetSubString(body,63 + 3, 63 + 3));

 Ignore my advice in the post above, both start position and end position should be the same for the llGetSubString.

Link to comment
Share on other sites

Once you've confirmed that this test works - i.e., that the server while on is reporting '1' and the while off is reporting '0', you can finally update the if in your http_response from

 

        if(status != 200)

to

if(status != 200 || llGetSubString(body,63 + 3,63 + 3) == "0")

This new test basically says "if the [value identified in the llOwnerSay] is '0', then the stream is not running."

The two vertical lines represent a logical OR. A status check is still useful, since it's possible for the server to return a non-200 status code (it just isn't giving a non-200 code when you want it to).

Once you've confirmed that this test changes the colour - matching with the llOwnerSay output saying '0', you can remove the llOwnerSay.

Link to comment
Share on other sites

I posted twice, so I'm posting here again to remind you to look at my response above.

The llOwnerSay seems to work as anticipated, go ahead and add the changes to the if statement inside http_response. :)

Unfortunately it won't be possible to test if the stream is ON but no music is playing - the page only changes when the stream is either ON or OFF.

Link to comment
Share on other sites

I changed 

 

if(status != 200 || llGetSubString(body,63 + 3,63 + 3 = "0")

to

if(status != 200 || llGetSubString(body,63 + 3,63 + 3 = "0"))

to close off the brackets, but I still get a syntax error "37:62 ERROR: Syntax Error". I get the syntax eror rwith the brackets unclosed too.

 

Link to comment
Share on other sites

It still does nothing. Here is the complete code as it stands. You've been a great help, I'm really sorry to take up your time, but I'm incompetent :)

 

// leave music_url blank if set in object's descriptionstring music_url = "http://66.7.214.7:8064"; key HTTPRequest;string feed;string URL;string currSongTitle;string lastSongTitle;list feedList;default{    state_entry()    {        llSetText(" ",<1,1,1>,1);        llSetTimerEvent(5.0);            if(music_url)         {            URL = music_url;        } else {            URL = llGetObjectDesc();        }    }    timer()    {        HTTPRequest=llHTTPRequest(URL + "/7.html HTTP/1.0\nUser-Agent: LSL Script (Mozilla Compatible)\n\n",[],"");    }http_response(key k,integer status, list meta, string body)    {        if(status != 200 || llGetSubString(body,63 + 3,63 + 3) == "0")    //FIXED KEY TEST, AS POST ABOVE FROM Nova Convair        {            llSetColor(<0.067, 0.067, 0.067>, ALL_SIDES);             //REMOVED 'return;'        }        else //LINE ADDED        { //LINE ADDED        feed = llGetSubString(body,llSubStringIndex(body, "<body>") + llStringLength("<body>"), llSubStringIndex(body,"</body>") - 1);        feedList = llParseString2List(feed,[","],[]);        currSongTitle = llList2String(feedList,6);        integer length = llGetListLength(feedList);                if(llList2String(feedList,7))        {            integer a = 7;            for(; a<length; ++a)            {                currSongTitle += ", " + llList2String(feedList,a);            }        }                 if (currSongTitle != lastSongTitle)        {            llSetText(currSongTitle,<1,1,1>,0.75);            llSetColor(<1.000, 1.000, 1.000>, ALL_SIDES);             lastSongTitle = currSongTitle;        }       }  //LINE ADDED    }}

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3689 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...