Skip to content

Commit

Permalink
review code and support jsb
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualSJ committed Sep 6, 2016
1 parent 4a38d84 commit 2bc8764
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 126 deletions.
20 changes: 14 additions & 6 deletions cocos2d/audio/CCAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var touchPlayList = [
];

var Audio = function (src) {
this._src = src;
this.src = src;
this._audioType = Audio.Type.UNKNOWN;
this._element = null;

Expand All @@ -42,6 +42,7 @@ var Audio = function (src) {
Audio.Type = {
DOM: 'AUDIO',
WEBAUDIO: 'WEBAUDIO',
NATIVE: 'NATIVE',
UNKNOWN: 'UNKNOWN'
};

Expand All @@ -54,8 +55,8 @@ Audio.State = {

(function (proto) {

proto.startLoad = function () {
var src = this._src,
proto.preload = function () {
var src = this.src,
audio = this;
var item = cc.loader.getItem(src);

Expand All @@ -80,6 +81,13 @@ Audio.State = {
}
list.push(callback);
};
proto.once = function (event, callback) {
var onceCallback = function (elem) {
callback.call(this, elem);
this.off(event, onceCallback);
};
this.on(event, onceCallback);
};
proto.emit = function (event) {
var list = this._eventList[event];
if (!list) return;
Expand Down Expand Up @@ -113,7 +121,7 @@ Audio.State = {
this.emit('ended', this);
}, this);
} else {
this._element = new webAudioElement(elem);
this._element = new WebAudioElement(elem);
this._audioType = Audio.Type.WEBAUDIO;
elem.onended = function () {
this.emit('ended', this);
Expand Down Expand Up @@ -211,7 +219,7 @@ Audio.State = {
})(Audio.prototype);

// Encapsulated WebAudio interface
var webAudioElement = function (buffer) {
var WebAudioElement = function (buffer) {
this._context = cc.sys.__audioSupport.context;
this._buffer = buffer;
this._volume = this._context['createGain']();
Expand Down Expand Up @@ -348,6 +356,6 @@ var webAudioElement = function (buffer) {
return num;
});

})(webAudioElement.prototype);
})(WebAudioElement.prototype);

module.exports = cc.Audio = Audio;
9 changes: 3 additions & 6 deletions cocos2d/audio/CCAudioEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var audioEngine = {
audio.play();
};
audio.on('load', callback);
audio.startLoad();
audio.preload();

return audio.instanceId;
},
Expand All @@ -102,9 +102,8 @@ var audioEngine = {
setLoop: function (audioID, loop) {
var audio = getAudioFromId(audioID);
if (!audio || !audio.setLoop)
return loop;
return;
audio.setLoop(loop);
return loop;
},

/**
Expand Down Expand Up @@ -234,19 +233,17 @@ var audioEngine = {
* @method getState
* @param {Number} audioID audio id.
* @param {Function} callback loaded callback.
* @return {Boolean} Whether set successfully.
* @example
* //example
* cc.audioEngine.setFinishCallback(id, function () {});
*/
setFinishCallback: function (audioID, callback) {
var audio = getAudioFromId(audioID);
if (!audio)
return false;
return;

audio.off('ended');
audio.on('ended', callback);
return true;
},

/**
Expand Down
5 changes: 1 addition & 4 deletions cocos2d/core/CCGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ if (!(CC_EDITOR && Editor.isMainProcess)) {
}

var audioEngine = cc.audioEngine = require('../audio/CCAudioEngine');
var isMusicPlaying = false;

/**
* !#en An object to boot the game.
Expand Down Expand Up @@ -255,7 +254,6 @@ var game = {
this._paused = true;
// Pause audio engine
if (audioEngine) {
isMusicPlaying = true;
audioEngine.pauseAll();
}
// Pause main loop
Expand All @@ -274,8 +272,7 @@ var game = {
if (!this._paused) return;
this._paused = false;
// Resume audio engine
if (audioEngine && isMusicPlaying) {
isMusicPlaying = false;
if (audioEngine) {
audioEngine.resumeAll();
}
// Resume main loop
Expand Down
23 changes: 14 additions & 9 deletions cocos2d/core/components/CCAudioSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var AudioSource = cc.Class({
},

ctor: function () {
this.audio = new cc.Audio(this._clip);
this.audio = new cc.Audio();
},

properties: {
Expand Down Expand Up @@ -88,8 +88,8 @@ var AudioSource = cc.Class({
set: function (value) {
this._clip = value;
this.audio.src = this._clip;
if (this.audio.startLoad) {
this.audio.startLoad();
if (this.audio.preload) {
this.audio.preload();
}
},
url: cc.AudioClip,
Expand Down Expand Up @@ -197,12 +197,17 @@ var AudioSource = cc.Class({
* @method play
*/
play: function () {
if ( this._clip ) {
var volume = this._mute ? 0 : this._volume;
this.audio.setLoop(this._loop);
this.audio.setVolume(volume);
this.audio.play();
}
if ( !this._clip ) return;

var volume = this._mute ? 0 : this._volume;
var audio = this.audio;
audio.src = this._clip;
audio.setLoop(this._loop);
audio.setVolume(volume);
audio.once('load', function () {
audio.play();
});
audio.preload();
},

/**
Expand Down
Loading

0 comments on commit 2bc8764

Please sign in to comment.