Commit 4df8c6a9 authored by jeff's avatar jeff

修改音乐组件播放模式

parent 77bc0c82
...@@ -36,7 +36,7 @@ cc.Class({ ...@@ -36,7 +36,7 @@ cc.Class({
init(url) { init(url) {
this.stopAudio() this.onEvent()
this.playBtn.active = true this.playBtn.active = true
this.pauseBtn.active = false this.pauseBtn.active = false
cc.assetManager.loadRemote(url, (err, audioClip) => { cc.assetManager.loadRemote(url, (err, audioClip) => {
...@@ -46,21 +46,31 @@ cc.Class({ ...@@ -46,21 +46,31 @@ cc.Class({
this.label.string = '0:00/' + this.transTime(this._totoalTime) this.label.string = '0:00/' + this.transTime(this._totoalTime)
this.tiao.children[0].getComponent(cc.Sprite).fillRange = 0 this.tiao.children[0].getComponent(cc.Sprite).fillRange = 0
this.tiao.children[1].x = 12 this.tiao.children[1].x = 12
this.onAudioFinish()
}) })
}, },
onAudioFinish() {
if (this._audioId != null) {
this.pauseBtn.active = false
this.playBtn.active = true
}
this._play = false
this._nowTime = 0
this._audioId = cc.audioEngine.play(this.clip)
cc.audioEngine.pause(this._audioId)
cc.audioEngine.setFinishCallback(this._audioId, this.onAudioFinish.bind(this))
},
onTouchStart() { onTouchStart() {
if (this._audioId == null) return
if (this._play) { if (this._play) {
this._pause = true this._pause = true
this._play = false this._play = false
cc.audioEngine.pause(this._audioId) cc.audioEngine.pause(this._audioId)
} }
}, },
/** 触摸移动 */ /** 触摸移动 */
onTouchMove(e) { onTouchMove(e) {
if (this._audioId == null) return
let dis = e.getPreviousLocation().sub(e.getLocation()) let dis = e.getPreviousLocation().sub(e.getLocation())
dis = this.moveBtn.x - dis.x dis = this.moveBtn.x - dis.x
if (dis < 12) dis = 12 if (dis < 12) dis = 12
...@@ -86,21 +96,15 @@ cc.Class({ ...@@ -86,21 +96,15 @@ cc.Class({
this.pauseBtn.active = true this.pauseBtn.active = true
this.playBtn.active = false this.playBtn.active = false
this._play = true this._play = true
if (this._audioId != null) {
cc.audioEngine.resume(this._audioId) cc.audioEngine.resume(this._audioId)
} else {
this._audioId = cc.audioEngine.play(this.clip)
cc.audioEngine.setFinishCallback(this._audioId, this.stopAudio.bind(this));
this._nowTime = 0
}
cc.systemEvent.emit('stopWordsMusic') cc.systemEvent.emit('stopWordsMusic')
}, },
onPause() { onPause() {
this.pauseBtn.active = false this.pauseBtn.active = false
this.playBtn.active = true this.playBtn.active = true
this._play = false this._play = false
this._pause = true
cc.audioEngine.pause(this._audioId) cc.audioEngine.pause(this._audioId)
}, },
...@@ -117,29 +121,7 @@ cc.Class({ ...@@ -117,29 +121,7 @@ cc.Class({
onEvent() { onEvent() {
if (!this._play) return if (!this._play) return
this.onStopAudio()
},
onStopAudio() {
if (this._nowTime < this._totoalTime) {
this.onPause() this.onPause()
} else {
this.stopAudio()
}
},
stopAudio() {
if (this._audioId != null) {
this._audioId = null
cc.audioEngine.stop(this._audioId)
this._play = false
this.pauseBtn.active = false
this.playBtn.active = true
// this._nowTime = 0
// this.label.string = this.transTime(this._nowTime) + '/' + this.transTime(this._totoalTime)
// this.tiao.children[0].getComponent(cc.Sprite).fillRange = 0
// this.tiao.children[1].x = 12
}
}, },
/** 秒转分钟 */ /** 秒转分钟 */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment