{"id":658,"date":"2019-12-08T16:23:19","date_gmt":"2019-12-08T08:23:19","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=658"},"modified":"2019-12-08T16:23:19","modified_gmt":"2019-12-08T08:23:19","slug":"egret%e5%9f%ba%e7%a1%80-%e9%9f%b3%e9%a2%91%e6%92%ad%e6%94%be","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=658","title":{"rendered":"egret\u57fa\u7840-\u97f3\u9891\u64ad\u653e"},"content":{"rendered":"<p>\u5148\u5b9a\u4e49\u4e00\u4e2aSoundExample\u7684\u7c7b\uff0c\u7ee7\u627f\u81eaegret.DisplayObjectContainer<\/p>\n<p>\u65b9\u6cd5\u4e00\uff1a<br \/>\n<code><br \/>\nclass SoundExample extends egret.DisplayObjectContainer {<br \/>\n    public constructor() {<br \/>\n        super();<br \/>\n        this.once(egret.Event.ADDED_TO_STAGE, this.onAddtoStage, this);<br \/>\n    }<br \/>\n    private onAddtoStage() {<br \/>\n        this.startLoad();<br \/>\n    }<br \/>\n    private startLoad(): void {<br \/>\n        var sound: egret.Sound = new egret.Sound();<br \/>\n        sound.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);<br \/>\n        sound.load(\"resource\/assets\/sound\/sound_bg.mp3\");<br \/>\n    }<br \/>\n    private onLoadComplete(event: egret.Event): void {<br \/>\n        \/\/\u83b7\u53d6\u52a0\u8f7d\u5230\u7684 Sound \u5bf9\u8c61<br \/>\n        var sound: egret.Sound = <egret.Sound>event.target;<br \/>\n        this.sound = sound;<br \/>\n        \/\/\u4e00\u4e2a\u7b80\u5355\u7684\u64ad\u653e\u6309\u94ae<br \/>\n        var btn = new egret.Sprite();<br \/>\n        btn.graphics.beginFill(0x18f7ff);<br \/>\n        btn.graphics.drawRoundRect(0, 0, 80, 40, 5, 5);<br \/>\n        btn.graphics.endFill();<br \/>\n        btn.touchEnabled = true;<br \/>\n        btn.anchorOffsetX = btn.width \/ 2;<br \/>\n        btn.x = this.stage.stageWidth \/ 2;<br \/>\n        btn.anchorOffsetY = btn.height \/ 2;<br \/>\n        btn.y = this.stage.stageHeight \/ 2;<br \/>\n        \/\/\u76d1\u542c\u6309\u94ae\u7684\u89e6\u6478\u4e8b\u4ef6<br \/>\n        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTouch, this);<br \/>\n        this.addChild(btn);<br \/>\n    }<br \/>\n    private sound: egret.Sound;<br \/>\n    private soundChannel: egret.SoundChannel;<\/p>\n<p>    private onTouch(event: egret.Event) {<br \/>\n        var sound = this.sound;<br \/>\n        \/\/\u83b7\u53d6\u97f3\u9891\u957f\u5ea6<br \/>\n        var channel: egret.SoundChannel = this.soundChannel;<br \/>\n        if (channel) {<br \/>\n            \/\/\u8c03\u7528soundChannel\u5bf9\u8c61\u7684stop\u65b9\u6cd5\u505c\u6b62\u64ad\u653e\u97f3\u9891<br \/>\n            console.log(channel);<br \/>\n            channel.stop();<br \/>\n            this.soundChannel = null;<br \/>\n            return;<br \/>\n        }<br \/>\n        \/\/\u4f7f\u7528SoundChannel\u64ad\u653e\u97f3\u9891<br \/>\n        channel = sound.play(0, -1);<br \/>\n        \/\/Egret 3.0.4 \u65b0\u589e\u83b7\u53d6\u97f3\u9891\u957f\u5ea6 length \u5c5e\u6027\u3002<br \/>\n        console.log(sound.length);<br \/>\n        channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);<br \/>\n        \/\/\u4fdd\u5b58soundChannel\u5bf9\u8c61<br \/>\n        this.soundChannel = channel;<br \/>\n    }<br \/>\n    private onSoundComplete(event: egret.Event): void {<br \/>\n        console.log(\"onSoundComplete\");<br \/>\n    }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>\u968f\u540e\u5728Main.ts\u91cc\u9762\u8fdb\u884c\u5b9e\u4f8b\u5316 \uff1a<\/p>\n<p>this.soundCurrent = new SoundTest();<br \/>\nthis.addChild(this.soundCurrent);<\/p>\n<p>\u65b9\u6cd5\u4e8c<br \/>\n\u8fd9\u91cc\u5148\u5b9a\u4e49\u4e00\u4e2aSoundTest\u7c7b\uff0c\u7136\u540e\u4e5f\u662f\u7ee7\u627fegret.DisplayObjectContainer<br \/>\n<code><br \/>\nclass SoundTest extends egret.DisplayObjectContainer {<br \/>\n    public constructor() {<br \/>\n        super();<br \/>\n    }<br \/>\n    public loadSoundRES(url) {<br \/>\n        var sound: egret.Sound = RES.getRes(url);<br \/>\n        sound.play(0, 1);<br \/>\n    }<br \/>\n    public loadSound(url) {<br \/>\n        \/\/\u521b\u5efa Sound \u5bf9\u8c61<br \/>\n        var sound = new egret.Sound();<br \/>\n        \/\/\u6dfb\u52a0\u52a0\u8f7d\u5b8c\u6210\u4fa6\u542c<br \/>\n        sound.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);<br \/>\n        \/\/\u5f00\u59cb\u52a0\u8f7d<br \/>\n        sound.load(url);<br \/>\n    }<br \/>\n    public onLoadComplete(event: egret.Event): void {<br \/>\n        \/\/\u83b7\u53d6\u52a0\u8f7d\u5230\u7684 Sound \u5bf9\u8c61<br \/>\n        var sound: egret.Sound = <egret.Sound>event.target;<br \/>\n        \/\/\u64ad\u653e\u97f3\u4e50<br \/>\n        console.log(sound)<br \/>\n        var channel: egret.SoundChannel = sound.play(0, 1);<br \/>\n        channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);<br \/>\n    }<\/p>\n<p>    public onSoundComplete(event: egret.Event): void {<br \/>\n        egret.log(\"onSoundComplete\");<br \/>\n    }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>\u5f53\u8c03\u7528\u65f6\u5019 \u4f9d\u65e7\u662f\u5728Main.ts \u91cc\u9762\u8fdb\u884c\u5b9e\u4f8b\u5316 \u540c\u65f6\u4f20\u5165\u60f3\u8981\u64ad\u653e\u7684\u97f3\u9891url:<br \/>\nsky.touchEnabled = true;<br \/>\nthis.soundCurrent = new SoundTest();<br \/>\nthis.addChild(this.soundCurrent);<br \/>\nsky.addEventListener(egret.TouchEvent.TOUCH_TAP, this.playSoundBg, this)<br \/>\n private playSoundBg() {<br \/>\n        var url: string = &#8220;sound_bg_mp3&#8221;;<br \/>\n        this.soundCurrent.loadSoundRES(url)<br \/>\n        console.log(url)<br \/>\n    }<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u5b9a\u4e49\u4e00\u4e2aSoundExample\u7684\u7c7b\uff0c\u7ee7\u627f\u81eaegret.DisplayObjectContainer \u65b9\u6cd5\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":144,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[15],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/658"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=658"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/658\/revisions"}],"predecessor-version":[{"id":659,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/658\/revisions\/659"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/media\/144"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}