Skip to content

Commit

Permalink
script.aculo.us: Added Sound.play(url,options) in new sound.js file, …
Browse files Browse the repository at this point in the history
…based on code by Jules Gravinese, used with permission.

git-svn-id: http://svn.rubyonrails.org/rails/spinoffs/scriptaculous@6295 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
madrobby committed Mar 3, 2007
1 parent d51f892 commit 59a3dd6
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG
@@ -1,5 +1,23 @@
*SVN*

* Added Sound.play(url,options) in new sound.js file. scriptaculous.js automatically includes this file.
Based on code by Jules Gravinese, used with permission.

The sound player uses native sound support on IE, and falls back to using <embed> on other browsers,
which means it uses QuickTime for most cases. The recommended format to use is MP3.

Examples:
Sound.play('blah.mp3');
// --> plays sound immediately in 'global' track
Sound.play('blah.mp3',{replace:true});
// --> stop playing all sounds in 'global' track, and play new sound
Sound.play('blah.mp3',{track:'mytrack'});
// --> places the sound in the 'mytrack' track
Sound.play('blah.mp3',{track:'mytrack',replace:true});
// --> stop playing all sounds in 'mytrack' track, and play new sound

The sound effect used in the functional test is "Sword being drawn" by James Greever, released as freeware.

* Various effects engine optimizations [Tobie Langel, Thomas Fuchs]

* Make Ajax.InPlaceEditor more customizable: [thx godlie]
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -37,7 +37,8 @@ SRC_FILES = FileList[
'src/unittest.js',
'src/builder.js',
'src/slider.js',
'src/unittest.js',
'src/sound.js',
'src/unittest.js'
]

RAILS_FILES = FileList[
Expand Down
2 changes: 1 addition & 1 deletion src/scriptaculous.js
Expand Up @@ -40,7 +40,7 @@ var Scriptaculous = {
}).each( function(s) {
var path = s.src.replace(/scriptaculous\.js(\?.*)?$/,'');
var includes = s.src.match(/\?.*load=([a-z,]*)/);
(includes ? includes[1] : 'builder,effects,dragdrop,controls,slider').split(',').each(
(includes ? includes[1] : 'builder,effects,dragdrop,controls,slider,sound').split(',').each(
function(include) { Scriptaculous.require(path+include+'.js') });
});
}
Expand Down
52 changes: 52 additions & 0 deletions src/sound.js
@@ -0,0 +1,52 @@
// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
//
// Based on code created by Jules Gravinese (http://www.webveteran.com/)
//
// script.aculo.us is freely distributable under the terms of an MIT-style license.
// For details, see the script.aculo.us web site: http://script.aculo.us/

Sound = {
tracks: {},
template:
new Template('<embed id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>'),
play: function(url){
var options = Object.extend({
track: 'global', url: url, replace: false
}, arguments[1] || {});

if(options.replace && this.tracks[options.track]) {
$R(0, this.tracks[options.track].id).each(function(id){
var sound = $('sound_'+options.track+'_'+id);
sound.Stop && sound.Stop();
sound.remove();
})
this.tracks[options.track] = null;
}

if(!this.tracks[options.track])
this.tracks[options.track] = { id: 0 }
else
this.tracks[options.track].id++;

options.id = this.tracks[options.track].id;
if (Prototype.Browser.IE) {
var sound = document.createElement('bgsound');
sound.setAttribute('id','sound_'+options.track+'_'+options.id);
sound.setAttribute('src',options.url);
sound.setAttribute('loop','1');
sound.setAttribute('autostart','true');
$$('body')[0].appendChild(sound);
}
else
new Insertion.Bottom($$('body')[0], Sound.template.evaluate(options));
}
};

(function(){
if(Prototype.Browser.Gecko && (navigator.platform.indexOf("Win") > 0) && navigator.plugins) {
var hasQuicktime = false;
for (i=0; i<navigator.plugins.length; i++ )
if(navigator.plugins[i].name.indexOf("QuickTime") >= 0) hasQuicktime = true;
if(!hasQuicktime) Sound.play = function(){};
}
})();
3 changes: 3 additions & 0 deletions test/functional/index.html
Expand Up @@ -82,6 +82,9 @@ <h1>script.aculo.us Functional Tests</h1>
<ul>
<li><a href="texteffects_test.html" target="test">texteffects_test.html</a></li>
</ul>
<ul>
<li><a href="sound_test.html" target="test">sound_test.html</a></li>
</ul>

</body>
</html>
18 changes: 18 additions & 0 deletions test/functional/sound_test.html
@@ -0,0 +1,18 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>script.aculo.us Sound functional test file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="../../lib/prototype.js" type="text/javascript"></script>
<script src="../../src/scriptaculous.js" type="text/javascript"></script>
</head>
<body>
<h1>script.aculo.us Sound test file</h1>

<a href="#" onclick="Sound.play('sword.mp3'); return false">play sound (parallel)</a>

<a href="#" onclick="Sound.play('sword.mp3',{replace:true}); return false">play sound (overwrite)</a>

</body>
</html>
Binary file added test/functional/sword.mp3
Binary file not shown.

0 comments on commit 59a3dd6

Please sign in to comment.