<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zedia flash blog &#187; SoundTransform</title>
	<atom:link href="http://www.zedia.net/tag/soundtransform/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zedia.net</link>
	<description>Flash, ActionScript, SEO and everything in between</description>
	<lastBuildDate>Thu, 02 Feb 2012 17:58:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Fading out volume using TweenLite</title>
		<link>http://www.zedia.net/2008/fading-out-volume-using-tweenlite/</link>
		<comments>http://www.zedia.net/2008/fading-out-volume-using-tweenlite/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 03:31:47 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Sound]]></category>
		<category><![CDATA[SoundChannel]]></category>
		<category><![CDATA[SoundTransform]]></category>
		<category><![CDATA[TweenLite]]></category>
		<category><![CDATA[Volume]]></category>

		<guid isPermaLink="false">http://www.zedia.net/2008/fading-out-volume-using-tweenlite/</guid>
		<description><![CDATA[I showed in my previous post how to change the volume of a playing sound. In this post I am going to show how to tween that volume using TweenLite. The class we are going to do the tween on is the SoundTransform class. So we will first have to create our sound objects. 1 [...]]]></description>
			<content:encoded><![CDATA[<p>I showed in my previous post <a href="http://www.zedia.net/2008/a-little-bit-about-sound-using-the-sound-soundchannel-soundtransform-classes/" title="How to change the volume of a playing sound">how to change the volume of a playing sound</a>. In this post I am going to show how to tween that volume using <a href="http://blog.greensock.com/tweenliteas3/" title="TweenLite" target="_blank">TweenLite</a>.</p>
<p>The class we are going to do the tween on is the SoundTransform class. So we will first have to create our sound objects.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someSound = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MySound.mp3&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someChannel = <span style="color: #000000; font-weight: bold;">new</span> SoundChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someTransform = <span style="color: #000000; font-weight: bold;">new</span> SoundTransform<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//volume is set to the max</span>
&nbsp;
someChannel = someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">99999</span>, someTransform<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//The sound will start from 0 millisecond, will loop 99999 times and will use the someTransform SoundTransform</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//the next lines are for fading out the volume</span>
&nbsp;
TweenLite.<span style="color: #006600;">to</span><span style="color: #66cc66;">&#40;</span>someSound, <span style="color: #cc66cc;">1</span>, <span style="color: #66cc66;">&#123;</span>volume:<span style="color: #cc66cc;">0</span>, <span style="color: #0066CC;">onUpdate</span>:updateChannel, onComplete:stopSound<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> updateChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
  someChannel.<span style="color: #006600;">soundTransform</span> = someTransform;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> stopSound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
  someChannel.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, even if you modify the volume parameter of the SoundTranform it won&#8217;t do a thing. You also have to update the SoundChannel. You can&#8217;t also tween the SoundChannel.soundTransform.volume directly. It&#8217;s the same as when you <a href="http://www.zedia.net/2007/tweening-colors-using-colorinterpolatecolor/" title="Tween Colors using Color.interpolateColor">tween colors using Color.interpolateColor</a>, you have to modify an intermediate Object.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2008/fading-out-volume-using-tweenlite/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>A little bit about Sound &#124; Using the Sound, SoundChannel, SoundTransform classes</title>
		<link>http://www.zedia.net/2008/a-little-bit-about-sound-using-the-sound-soundchannel-soundtransform-classes/</link>
		<comments>http://www.zedia.net/2008/a-little-bit-about-sound-using-the-sound-soundchannel-soundtransform-classes/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 04:36:09 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Sound]]></category>
		<category><![CDATA[SoundChannel]]></category>
		<category><![CDATA[SoundTransform]]></category>
		<category><![CDATA[Volume]]></category>

		<guid isPermaLink="false">http://www.zedia.net/2008/a-little-bit-about-sound-using-the-sound-soundchannel-soundtransform-classes/</guid>
		<description><![CDATA[I didn&#8217;t really mess up with sounds in ActionScript 3 until recently. I can say that it changed a lot since ActionScript 2, it&#8217;s a bit more complex, but I&#8217;m pretty sure it gives more possibilities also. First, you can either load the sound from the library, or you can load it externally. If it [...]]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t really mess up with sounds in ActionScript 3 until recently. I can say that it changed a lot since ActionScript 2, it&#8217;s a bit more complex, but I&#8217;m pretty sure it gives more possibilities also.</p>
<p>First, you can either load the sound from the library, or you can load it externally. If it is from the library, you first have to click export for ActionScript in the property panel from that sound, and use the class name you gave it. Let say you gave it &#8220;MySound&#8221; as class name here is how you would simply make it play:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someSound:MySound =  <span style="color: #000000; font-weight: bold;">new</span> MySound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>If you want to load your sound from an external file it is a bit different but afterward it&#8217;s all the same:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someSound:<span style="color: #0066CC;">Sound</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MySound.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//it could also be done like this</span>
<span style="color: #000000; font-weight: bold;">var</span> someSound:<span style="color: #0066CC;">Sound</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
someSound.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MySound.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> ;</pre></td></tr></table></div>

<p>In the last example, the file MySound.mp3 has to be in the same folder as the HTML embedding the SWF because that what I indicate in the URLRequest.</p>
<p>If you use only the Sound class the only thing you can do is make the sound play, that&#8217;s it, no stop, no pause, no adjustment to the volume. For that you will have to use two other classes SoundChannel and SoundTransform.</p>
<p><strong>SoundChannel </strong></p>
<p>The SoundChannel class possess only one method and it&#8217;s the stop method. I guess you know what it does. here is how you use it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someSound:<span style="color: #0066CC;">Sound</span> =  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MySound.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someChannel:SoundChannel = <span style="color: #000000; font-weight: bold;">new</span> SoundChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
someChannel = someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
someChannel.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>The last bit of code is actually useless because it will stop the sound right after it started it but it is only to show how to use a SoundChannel. Fine but how do you pause a sound and than make it play again with only a stop() method? The SoundChannel class has a property called position which indicate where the play head is at. You have to save the value of the position property before calling the stop method. If you save to position property value after calling the stop method, it will be of value 0 which is of no interest. What you do with the value of the position property is you pass it as a parameter to the Sound.play() method. The first parameter, startTime, tell the Sound Object at which millisecond to start playing. It&#8217;s a bit confusing but this example should make it clearer:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someSound:<span style="color: #0066CC;">Sound</span> =  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MySound.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someChannel:SoundChannel = <span style="color: #000000; font-weight: bold;">new</span> SoundChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> somePosition:<span style="color: #0066CC;">Number</span>;
&nbsp;
someChannel = someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//this make the sound play</span>
somePosition = someChannel.<span style="color: #0066CC;">position</span>; <span style="color: #808080; font-style: italic;">//saves the value of the position property</span>
someChannel.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//pause or stop the sound</span>
someChannel = someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>somePosition<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//this make the sound play but it now start from the position at which it was stopped</span></pre></td></tr></table></div>

<p>The Sound.play() method also takes two other optional parameters. The first of the two is the loops parameter, it indicate how many times you want the sound to play in loop. If you want your sound to loop forever you just pass a really big number as parameter. if you want it to play just once you pass it 0. The second of the two (the third parameter really) is the SoundTransform parameter.</p>
<p><strong>SoundTransform</strong></p>
<p>The SoundTransform class as some properties and no methods, but I will only speak about the volume property here. The volume property is a number ranging from 0 to 1, 0 being silent and 1 being at full volume. You can either set it in the constructor or after the SoundTransform has been created. here is how you would make a sound play at half the maximum volume:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someSound:<span style="color: #0066CC;">Sound</span> =  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MySound.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someChannel:SoundChannel = <span style="color: #000000; font-weight: bold;">new</span> SoundChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someTransform:SoundTransfrom = <span style="color: #000000; font-weight: bold;">new</span> SoundTransform<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// the 0.5 value here tells it to set volume to half the maximum value</span>
&nbsp;
someChannel = someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, someTransform<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//the first 0 is startTime, the second is the number of loop, and finally we pass our SoundTransform</span></pre></td></tr></table></div>

<p>If we wanted to change the volume as the sound is playing we would do it in this way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> someSound:<span style="color: #0066CC;">Sound</span> =  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;MySound.mp3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someChannel:SoundChannel = <span style="color: #000000; font-weight: bold;">new</span> SoundChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> someTransform:SoundTransfrom = <span style="color: #000000; font-weight: bold;">new</span> SoundTransform<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// the 0.5 value here tells it to set volume to half the maximum value</span>
&nbsp;
someChannel = someSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, someTransform<span style="color: #66cc66;">&#41;</span>;
someTransform.<span style="color: #006600;">volume</span> = <span style="color: #cc66cc;">1</span>;
someChannel.<span style="color: #006600;">soundTransform</span> = someTransform;</pre></td></tr></table></div>

<p>This would start playing the the sound at half the volume to set it afterward at maximum volume. Well that is as far as I go with sound, with that you can do most of the common tasks. In a future post I will show <a href="http://www.zedia.net/2008/fading-out-volume-using-tweenlite/" title="How to use TweenLite to fade the volume to zero">how to use TweenLite to fade the volume to zero</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2008/a-little-bit-about-sound-using-the-sound-soundchannel-soundtransform-classes/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

