連載8回目は、
- 時間のかかるアップロードの終了時に音を鳴らす
(このときブラウザのウィンドウが隠れていても大丈夫です) - チャットのお知らせ音
- 認証失敗のビープ音
- 後戻りできない処理の警告音
- 短い音声による案内
内部的には、
なぜFlashを使わないのかについては、
並行に音を鳴らすこともできます。これには、
次のように使います。
// 音声を再生する。トラックは'global'
Sound.play('blah.mp3');
// 'global'にある全ての音を止めてから、改めて音声を再生する。
Sound.play('blah.mp3',{replace:true});
// 'mytrack'で再生する。(この関数を同様に何度も呼び出すことで、複数の音を登録できます)
Sound.play('blah.mp3',{track:'mytrack'});
// 'mytrack'にある全ての音を止めてから、改めて音声を再生する。
Sound.play('blah.mp3',{track:'mytrack',replace:true});
それではコードを見ていきましょう。
0001:// script.aculo.us sound.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008
0002:
0003:// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
0004://
0005:// Based on code created by Jules Gravinese (http://www.webveteran.com/)
0006://
0007:// script.aculo.us is freely distributable under the terms of an MIT-style license.
0008:// For details, see the script.aculo.us web site: http://script.aculo.us/
0009:
1~9行目は、
Sound
0010:Sound = {
0011: tracks: {},
0012: _enabled: true,
11行目の、
12行目の、
0013: template:
0014: new Template('<embed style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>'),
13、
0015: enable: function(){
0016: Sound._enabled = true;
0017: },
0018: disable: function(){
0019: Sound._enabled = false;
0020: },
15~17行目の、
18~20行目の、
0021: play: function(url){
0022: if(!Sound._enabled) return;
0023: var options = Object.extend({
0024: track: 'global', url: url, replace: false
0025: }, arguments[1] || {});
0026:
0027: if(options.replace && this.tracks[options.track]) {
0028: $R(0, this.tracks[options.track].id).each(function(id){
0029: var sound = $('sound_'+options.track+'_'+id);
0030: sound.Stop && sound.Stop();
0031: sound.remove();
0032: })
0033: this.tracks[options.track] = null;
0034: }
0035:
0036: if(!this.tracks[options.track])
0037: this.tracks[options.track] = { id: 0 }
0038: else
0039: this.tracks[options.track].id++;
0040:
0041: options.id = this.tracks[options.track].id;
0042: $$('body')[0].insert(
0043: Prototype.Browser.IE ? new Element('bgsound',{
0044: id: 'sound_'+options.track+'_'+options.id,
0045: src: options.url, loop: 1, autostart: true
0046: }) : Sound.template.evaluate(options));
0047: }
0048:};
0049:
21~48行目の、
22行目で、
23行目で、
27行目で、
28行目で、
29行目で、
30行目で、
31行目で、
33行目で、
ここから、
36~39行目で、
42~46行目で、
0050:if(Prototype.Browser.Gecko && navigator.userAgent.indexOf("Win") > 0){
0051: if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('QuickTime') != -1 }))
0052: Sound.template = new Template('<object id="sound_#{track}_#{id}" width="0" height="0" type="audio/mpeg" data="#{url}"/>')
0053: else
0054: Sound.play = function(){}
0055:}
50行目で、
51行目で、
52行目で、
54行目で、