Skip to content

Commit

Permalink
fix error when set volume as a string value (cocos#3455)
Browse files Browse the repository at this point in the history
  • Loading branch information
PPpro authored and pandamicro committed Nov 5, 2018
1 parent cf2d4d1 commit b568f39
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cocos2d/audio/CCAudioEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ let getAudioFromId = function (id) {
return _id2audio[id];
};

let handleVolume = function (volume) {
if (!volume) {
// set default volume as 1
volume = 1;
}
else if (typeof volume === 'string') {
volume = Number.parseFloat(volume);
}
return volume;
};

/**
* !#en cc.audioEngine is the singleton object, it provide simple audio APIs.
* !#zh
Expand Down Expand Up @@ -150,9 +161,7 @@ var audioEngine = {
}

audio.setLoop(loop || false);
if (typeof volume !== 'number') {
volume = 1;
}
volume = handleVolume(volume);
audio.setVolume(volume);
audio.play();

Expand Down Expand Up @@ -647,6 +656,7 @@ var audioEngine = {
* cc.audioEngine.setMusicVolume(0.5);
*/
setMusicVolume: function (volume) {
volume = handleVolume(volume);
var music = this._music;
music.volume = volume;
this.setVolume(music.id, music.volume);
Expand Down Expand Up @@ -690,6 +700,7 @@ var audioEngine = {
* cc.audioEngine.setEffectsVolume(0.5);
*/
setEffectsVolume: function (volume) {
volume = handleVolume(volume);
var musicId = this._music.id;
this._effect.volume = volume;
for (var id in _id2audio) {
Expand Down

0 comments on commit b568f39

Please sign in to comment.