...

View Full Version : Play sound if a certain term exists?



martin_75
06-18-2010, 12:39 PM
Hi Everyone

I don't have a clue what i'm doing with this and don't expect someone to just give me the code if it is actually possible (although it would be nice).

I've created a site and in the admin area the page will update and tell me if there is a new message from a user, I have this page automatically refreshing every 5 minutes and wondered if it's possible to play a sound file if the term "unread" appears anywhere on the page?

Thanks for any help you can provide.

Philip M
06-18-2010, 05:53 PM
Playing a sound cross-browser is not easy. You shuld have a look at http://www.boutell.com/newfaq/creating/scriptsound.html
It may be simpler just to display a prominent message.

You can identify whether a word appears in the <body> as follows:-


<html>
<head>

<script type="text/javascript" >
var str2find = "unread"; // unmunged in the head
</script>

</head>

<body>

<p>This page is unread</p>

<script type = "text/javascript">

window.onload = function(){
var str = document.getElementsByTagName('body')[0].innerHTML;
//str = str.toLowerCase(); // to make case insensitive
//str2find=str2find.toLowerCase(); // to make case insensitive
var a = str.indexOf(str2find);
if (a >= 0) {
alert ("The BODY contains the string " + str2find);

}
else {
alert ("Sorry - this sequence of characters was not found.");
}
}
</script>

</body>
</html>


"A little sincerity is a dangerous thing, and a great deal of it is absolutely fatal". - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

martin_75
06-18-2010, 08:07 PM
Thank you very much Philip, I was able to use your code very easily on my site.

Playing the sound cross-browser isn't an issue really, I will be the only person that uses the admin area and will most probably always use the same browser.

I assume this is the area that I will need to change to get a sound to play


if (a >= 0) {
alert ("The BODY contains the string " + str2find);

}
else {
alert ("Sorry - this sequence of characters was not found.");
}

I have set up SoundManager as per the guide but I am unsure what I would replace "alert ("The BODY contains the string " + str2find);" with.

Philip M
06-19-2010, 07:36 AM
I have set up SoundManager as per the guide but I am unsure what I would replace "alert ("The BODY contains the string " + str2find);" with.

Sorry, I don't have any experience of Sound Manager but the command to play the sound should be placed, as you say, in place of
alert ("The BODY contains the string " + str2find);

After some experimentation, the following works in IE and FF:-


<html>
<head>

<script type="text/javascript">
var str2find = "unread"; // this must be in the <head>
</script>

</head>

<body>

<script type = "text/javascript">
function EvalSound(soundobj) {
var mysound = document.getElementById(soundobj);
mysound.Play();
}
</script>

<input type = "button" value = "Stop Music" onclick = "document.embeds['sound1'].stop()">

<p>This page is unread</p>

<script type = "text/javascript">

window.onload = function(){
var str = document.getElementsByTagName('body')[0].innerHTML;
//str = str.toLowerCase(); // to make case insensitive
//str2find=str2find.toLowerCase(); // to make case insensitive
var a = str.indexOf(str2find);
if (a >= 0) {
//alert ("The BODY contains the string " + str2find);
setTimeout("EvalSound('sound1')",200); // delay is essential
}
else {
alert ("Sorry - this sequence of characters was not found.");
// or, of course, do nothing
}
}
</script>

<embed src="beetfifth.mid" autostart="false" width=0 height=0 loop="true" id="sound1">

</body>
</html>

The advantage of a music file with a loop is that it will continue to play until acknowledged.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum