Commit 082b2e0c authored by jeff's avatar jeff

更新audio

parent ee97fab7
...@@ -8,16 +8,16 @@ cc.Class({ ...@@ -8,16 +8,16 @@ cc.Class({
tiao: cc.Node, tiao: cc.Node,
moveBtn: cc.Node moveBtn: cc.Node
}, },
onload() { onLoad() {
this._clip = null this._clip = null
this._play = false this._play = false
this._audioId = null
this._totoalTime = null this._totoalTime = null
this._nowTime = null this._nowTime = 0
this._pause = false this._pause = false
this.node.active = false this.clip = null
}, },
onEnable() { onEnable() {
this._nowTime = 0
cc.systemEvent.on('stopBgMusic', this.onEvent, this) cc.systemEvent.on('stopBgMusic', this.onEvent, this)
this.moveBtn.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this) this.moveBtn.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
this.moveBtn.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this) this.moveBtn.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
...@@ -37,38 +37,38 @@ cc.Class({ ...@@ -37,38 +37,38 @@ cc.Class({
init(url) { init(url) {
if (!this.au) {
this.au = new cc.AudioSource()
}
this.onEvent() this.onEvent()
this.playBtn.active = true this.node.active = false
this.pauseBtn.active = false
this.node.active =false
cc.assetManager.loadRemote(url, (err, audioClip) => { cc.assetManager.loadRemote(url, (err, audioClip) => {
if (err) return if (err) return
this.clip = audioClip this.clip = audioClip
this._nowTime = 0
this._totoalTime = audioClip.duration this._totoalTime = audioClip.duration
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() this.au.clip = this.clip
this.node.active = true this.node.active = true
}) })
}, },
onAudioFinish() { onAudioFinish() {
if (this._audioId != null) { if (this._play) {
this.pauseBtn.active = false this.pauseBtn.active = false
this.playBtn.active = true this.playBtn.active = true
this._play = false
} }
this._play = false
this._nowTime = 0 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._play) { if (this._play) {
this._pause = true this._pause = true
this._play = false this._play = false
cc.audioEngine.pause(this._audioId) this.au.pause()
} }
}, },
...@@ -90,24 +90,28 @@ cc.Class({ ...@@ -90,24 +90,28 @@ cc.Class({
if (this._pause) { if (this._pause) {
this._play = true this._play = true
this._pause = false this._pause = false
cc.audioEngine.resume(this._audioId) this.au.play()
} }
cc.audioEngine.setCurrentTime(this._audioId, this._totoalTime * (this.moveBtn.x - 12) / 364) this._nowTime = this._totoalTime * (this.moveBtn.x - 12) / 364
this.au.setCurrentTime(this._nowTime)
}, },
onPlay() { onPlay() {
if (this._play) return
this._play = true
this.pauseBtn.active = true this.pauseBtn.active = true
this.playBtn.active = false this.playBtn.active = false
this._play = true this.au.play()
cc.audioEngine.resume(this._audioId) this.au.setCurrentTime(this._nowTime)
cc.systemEvent.emit('stopWordsMusic') cc.systemEvent.emit('stopWordsMusic')
}, },
onPause() { onPause() {
if (!this._play) return
this._play = false
this.pauseBtn.active = false this.pauseBtn.active = false
this.playBtn.active = true this.playBtn.active = true
this._play = false this.au.stop()
cc.audioEngine.pause(this._audioId)
}, },
/** 定时回调 */ /** 定时回调 */
...@@ -115,10 +119,16 @@ cc.Class({ ...@@ -115,10 +119,16 @@ cc.Class({
if (!this._play) return if (!this._play) return
this._nowTime += dt this._nowTime += dt
this.label.string = this.transTime(this._nowTime) + '/' + this.transTime(this._totoalTime) this.label.string = this.transTime(this._nowTime) + '/' + this.transTime(this._totoalTime)
if (this._nowTime > this._totoalTime) this._nowTime = this._totoalTime if (this._nowTime > this._totoalTime) {
this._nowTime = this._totoalTime
}
let scale = this._nowTime / this._totoalTime let scale = this._nowTime / this._totoalTime
this.tiao.children[0].getComponent(cc.Sprite).fillRange = scale this.tiao.children[0].getComponent(cc.Sprite).fillRange = scale
this.tiao.children[1].x = 12 + 364 * scale this.tiao.children[1].x = 12 + 364 * scale
if (this._nowTime == this._totoalTime) {
this.onPause()
this._nowTime = 0
}
}, },
onEvent() { onEvent() {
......
...@@ -1948,7 +1948,7 @@ ...@@ -1948,7 +1948,7 @@
"__id__": 57 "__id__": 57
} }
], ],
"_active": true, "_active": false,
"_components": [ "_components": [
{ {
"__id__": 59 "__id__": 59
......
...@@ -295,6 +295,7 @@ cc.Class({ ...@@ -295,6 +295,7 @@ cc.Class({
let url = data.bgItem.bgAudioUrl let url = data.bgItem.bgAudioUrl
if (url && url != '') { if (url && url != '') {
this.bgAudioNode.active =true
this.bgAudioNode.getComponent('audioItem').init(url) this.bgAudioNode.getComponent('audioItem').init(url)
} else { } else {
this.bgAudioNode.active = false this.bgAudioNode.active = false
......
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