{"id":888,"date":"2012-06-11T06:38:49","date_gmt":"2012-06-11T13:38:49","guid":{"rendered":"http:\/\/www.partnersinrhyme.com\/blog\/?p=888"},"modified":"2012-06-16T06:48:09","modified_gmt":"2012-06-16T13:48:09","slug":"flash-audio-sound-fade-in-fade-out","status":"publish","type":"post","link":"https:\/\/www.partnersinrhyme.com\/blog\/flash-audio-sound-fade-in-fade-out\/","title":{"rendered":"Flash Audio: Sound Fade In \/ Fade Out"},"content":{"rendered":"<p>Assuming you have successfully completed the previous tutorials I expect that I do not have to show you how to create buttons, import sounds, add graphics to sounds etc. In this tutorial we need to concetrate on the ActionScript that fades in\/out the played sound. <\/p>\n<p>Follow these steps to do this:<\/p>\n<p>1.&nbsp;&nbsp; &nbsp;Import a sound loop to you Flash Movie<br \/>2.&nbsp;&nbsp; &nbsp;Give it a linkage ID &lsquo;myTrack&rsquo;<br \/>3.&nbsp;&nbsp; &nbsp;Create to buttons. One with a plus sign inside and the other one with a minus sign inside. Place the on the stage like on the image 1. <\/p>\n<p><img hspace=\"10\" vspace=\"10\" src=\"http:\/\/www.partnersinrhyme.com\/contact\/images\/flashfade\/image_1.jpg\" alt=\"Flash tutorial\" \/><\/p>\n<p>4.&nbsp;&nbsp; &nbsp;The plus button should have the instance name &lsquo;fadeIn_btn&rsquo;, the minus button &lsquo;fadeOut_btn&rsquo;. <\/p>\n<p>The button should be placed on the layer &lsquo;Content&rsquo;. Above this layer, create a layer called &lsquo;Actions&rsquo;, select the first frame and open the Actions Panel (press F9). Paste the following code:<\/p>\n<p>var mySound:Sound = new Sound();<br \/>mySound.attachSound(&quot;myTrack&quot;);<br \/>mySound.start();<br \/>var soundStarted:Boolean = true;<br \/>var lastPosition:Number = 0;<br \/>var id:Number;<br \/>fadeIn_btn.onRelease = fadeInSound;<br \/>function fadeInSound():Void {<br \/>&nbsp;&nbsp; &nbsp;if (id) {<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;clearInterval(id);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;mySound.start(lastPosition);<br \/>&nbsp;&nbsp; &nbsp;var volume = mySound.getVolume();<br \/>&nbsp;&nbsp; &nbsp;id = setInterval(fadeIn, 10);<br \/>&nbsp;&nbsp; &nbsp;function fadeIn():Void {<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mySound.setVolume(volume++);<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (volume&gt;=100) {<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;clearInterval(id);<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;}<br \/>}<br \/>fadeOut_btn.onRelease = fadeOutSound;<br \/>function fadeOutSound():Void {<br \/>&nbsp;&nbsp; &nbsp;if (id) {<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;clearInterval(id);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;var volume = mySound.getVolume();<br \/>&nbsp;&nbsp; &nbsp;id = setInterval(fadeOut, 10);<br \/>&nbsp;&nbsp; &nbsp;function fadeOut():Void {<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mySound.setVolume(volume&#8211;);<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (volume&lt;=0) {<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;clearInterval(id);<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mySound.stop();<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lastPosition = Math.round(mySound.position\/1000);<br \/>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;}<br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assuming you have successfully completed the previous tutorials I expect that I do not have to show you how to create buttons, import sounds, add graphics to sounds etc. In this tutorial we need to concetrate on the ActionScript that fades in\/out the played sound. Follow these steps to do this: 1.&nbsp;&nbsp; &nbsp;Import a sound [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[],"_links":{"self":[{"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/posts\/888"}],"collection":[{"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/comments?post=888"}],"version-history":[{"count":3,"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/posts\/888\/revisions"}],"predecessor-version":[{"id":904,"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/posts\/888\/revisions\/904"}],"wp:attachment":[{"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/media?parent=888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/categories?post=888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.partnersinrhyme.com\/blog\/wp-json\/wp\/v2\/tags?post=888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}