Skip to content

Commit 298a456

Browse files
committed
Audio play flags move to cc.Audio
1 parent b54750a commit 298a456

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

cocos2d/audio/CCAudio.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
* Encapsulate DOM and webAudio
8484
*/
8585
cc.Audio = cc.Class.extend({
86+
interruptPlay: false,
8687
src: null,
8788
_element: null,
8889
_AUDIO_TYPE: "AUDIO",
@@ -110,7 +111,10 @@ cc.Audio = cc.Class.extend({
110111
},
111112

112113
play: function (offset, loop) {
113-
if (!this._element) return;
114+
if (!this._element) {
115+
this.interruptPlay = false;
116+
return;
117+
}
114118
this._element.loop = loop;
115119
this._element.play();
116120
if (this._AUDIO_TYPE === 'AUDIO' && this._element.paused) {
@@ -131,7 +135,10 @@ cc.Audio = cc.Class.extend({
131135
},
132136

133137
stop: function () {
134-
if (!this._element) return;
138+
if (!this._element) {
139+
this.interruptPlay = true;
140+
return;
141+
}
135142
this._element.pause();
136143
try {
137144
this._element.currentTime = 0;
@@ -140,12 +147,18 @@ cc.Audio = cc.Class.extend({
140147
},
141148

142149
pause: function () {
143-
if (!this._element) return;
150+
if (!this._element) {
151+
this.interruptPlay = true;
152+
return;
153+
}
144154
this._element.pause();
145155
},
146156

147157
resume: function () {
148-
if (!this._element) return;
158+
if (!this._element) {
159+
this.interruptPlay = false;
160+
return;
161+
}
149162
this._element.play();
150163
},
151164

@@ -503,7 +516,6 @@ cc.Audio.WebAudio.prototype = {
503516
return false;
504517
},
505518

506-
_waitLoadPlayFlag: true,
507519
/**
508520
* Play music.
509521
* @param {String} url The path of the music file without filename extension.
@@ -513,17 +525,15 @@ cc.Audio.WebAudio.prototype = {
513525
* cc.audioEngine.playMusic(path, false);
514526
*/
515527
playMusic: function(url, loop){
516-
this._waitLoadPlayFlag = true;
517528
var bgMusic = this._currMusic;
518529
if (bgMusic && bgMusic.getPlaying()) {
519530
bgMusic.stop();
520531
}
521532
var musicVolume = this._musicVolume;
522533
var audio = cc.loader.getRes(url);
523-
var self = this;
524534
if (!audio) {
525535
cc.loader.load(url, function () {
526-
if (self._waitLoadPlayFlag && !audio.getPlaying()) {
536+
if (!audio.getPlaying() && !audio.interruptPlay) {
527537
audio.setVolume(musicVolume);
528538
audio.play(0, loop || false);
529539
}
@@ -545,7 +555,6 @@ cc.Audio.WebAudio.prototype = {
545555
*/
546556
stopMusic: function(releaseData){
547557
var audio = this._currMusic;
548-
this._waitLoadPlayFlag = false;
549558
if (audio) {
550559
var list = cc.Audio.touchPlayList;
551560
for (var i=list.length-1; i>=0; --i) {
@@ -568,7 +577,6 @@ cc.Audio.WebAudio.prototype = {
568577
*/
569578
pauseMusic: function () {
570579
var audio = this._currMusic;
571-
this._waitLoadPlayFlag = false;
572580
if (audio)
573581
audio.pause();
574582
},

0 commit comments

Comments
 (0)