1 /**
  2  * [Javascript core part]: sound 扩展
  3  */
  4  
  5 /** 
  6  * @description
  7  * Package: jet.sound
  8  * 
  9  * Need package:
 10  * jet.core.js
 11  * 
 12  */
 13 Jx().$package(function(J){
 14     
 15     var $E = J.event;
 16     var soundObjectList = [];
 17 
 18     
 19     /**
 20      * @namespace
 21      * @name sound
 22      */
 23     J.sound = J.Class({
 24         /**
 25          * 声音类
 26          * @ignore
 27          * 
 28          * @param {string} url  mp3 url
 29          * @param {boolean} autoLoadAndPlay  加载完成自动播放
 30          * @param {boolean} needEventSupport  是否需要事件监听
 31          */
 32         init : function(url,autoLoadAndPlay,needEventSupport){
 33             var audio = this._audio = new J.Audio();
 34             soundObjectList.push(this);
 35             var context = this;
 36             if(needEventSupport){
 37                 audio.on('durationchange',function(){
 38                     $E.notifyObservers(context,'durationchange');
 39                 },false);
 40                 audio.on('timeupdate',function(){
 41                     $E.notifyObservers(context,'timeupdate');
 42                 },false);
 43                 audio.on('canplay',function(){
 44                     $E.notifyObservers(context,'canplay');
 45                 },false);
 46                 audio.on('ended',function(){
 47                     $E.notifyObservers(context,'ended');
 48                 },false);
 49                 audio.on('play',function(){
 50                     $E.notifyObservers(context,'play');
 51                 },false);
 52                 audio.on('pause',function(){
 53                     $E.notifyObservers(context,'pause');
 54                 },false);
 55                 audio.on('progress',function(){
 56                     $E.notifyObservers(context,'progress');
 57                 },false);
 58                 audio.on('error',function(){
 59                     $E.notifyObservers(context,'error');
 60                 },false);
 61             }
 62             if(autoLoadAndPlay){
 63                 audio.play(url);
 64             }else{
 65                 this._url = url;
 66             }
 67         },
 68         /*
 69          * @param {string} url: mp3 url,可选
 70          */
 71         load : function(url){
 72             if(url){
 73                 this._url = url;
 74             }
 75             this.play();
 76             this.pause();
 77         },
 78         
 79         getVolume : function(){
 80             return this._audio.getVolume();
 81         },
 82         setVolume : function(value){
 83             this._audio.setVolume();
 84             return true;
 85         },
 86         mute : function(){
 87             this._audio.setMute(true);
 88         },
 89         unMute : function(){
 90             this._audio.setMute(false);
 91         },
 92         isMute : function(){
 93             return this._audio.getMute();
 94         },
 95         play : function(){
 96             this._audio.play(this._url);
 97             delete this._url;
 98         },
 99         pause : function(){
100             this._audio.pause();
101         },
102         stop : function(){
103             this._audio.stop();
104         },
105         getDuration : function(){
106             return this._audio.getDuration();
107         },
108         getPosition : function(){
109             return this._audio.getPosition();
110         },
111         setPosition : function(value){
112             this._audio.setPosition(value);
113             return true;
114         },
115         buffered : function(){
116             return this._audio.getBuffered();
117         },
118         free : function(){
119             this._audio.free();
120             var index = J.array.indexOf(soundObjectList, this);
121             if(-1 !== index){
122                 soundObjectList.splice(index, 1);
123             }
124         }
125     });
126 
127     J.sound.init = function(){};
128     J.sound.isReady = true;
129     J.sound.Global = {
130         _mute : false,
131         getVolume : function(){
132             return 1;
133         },
134         setVolume : function(value){
135             for(var i in soundObjectList){
136                 soundObjectList[i] && soundObjectList[i].setVolume(value);
137             }
138             return true;
139         },
140         mute : function(){
141             this._mute = true;
142             for(var i in soundObjectList){
143                 soundObjectList[i] && soundObjectList[i].mute();
144             }
145         },
146         unMute : function(){
147             this._mute = false;
148             for(var i in soundObjectList){
149                 soundObjectList[i] && soundObjectList[i].unMute();
150             }
151         },
152         isMute : function(){
153             return this._mute;
154         },
155         removeAll : function(){
156             for(var obj; obj = soundObjectList.pop();){
157                 obj.free();
158             }
159         }
160     };
161 });
162