<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to Play Sound Files Sequentially</title>
	<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially</link>
	<description>Luck is the residue of good design.</description>
	<pubDate>Wed, 19 Nov 2008 04:21:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-286933</link>
		<pubDate>Fri, 10 Oct 2008 00:28:19 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-286933</guid>
					<description>Charles,

You wrote back on June 9, 2007, and I'm sorry to say I missed your reply until today!  At that time, you asked this:

&lt;blockquote&gt;Is there a way to play two sounds, one after the other, and embed the sounds in the swf instead of stream?&lt;/blockquote&gt;

The answer is yes on both counts, and the answers I gave GeePee and Kibo (the most recent to this reply to you) show you how it's done.  In those cases, I suggested the use of an array, but for two files you could just as easily do it like this.  Assuming your linkage identifiers are &quot;songA&quot; and &quot;songB&quot;:

&lt;pre&gt;&lt;code&gt;var audio:Sound = new Sound();
audio.attachSound(&amp;#34;songA&amp;#34;);
audio.start();

audio.onSoundComplete = function():Void {
  this.attachSound(&amp;#34;songB&amp;#34;);
  this.start();
  delete this.onSoundComplete;
};&lt;/code&gt;&lt;/pre&gt;

The &lt;code&gt;onSoundComplete&lt;/code&gt; handler is key:  when the first song ends, the next one is associated with the same &lt;code&gt;Sound&lt;/code&gt; instance by way of the &lt;code&gt;attachSound()&lt;/code&gt; method and is instructed to start playing &amp;#8212; and the event handler is deleted so that, at the end of the second song, Flash doesn't do anything else.</description>
		<content:encoded><![CDATA[<p>Charles,</p>
<p>You wrote back on June 9, 2007, and I&#8217;m sorry to say I missed your reply until today!  At that time, you asked this:</p>
<blockquote><p>Is there a way to play two sounds, one after the other, and embed the sounds in the swf instead of stream?</p></blockquote>
<p>The answer is yes on both counts, and the answers I gave GeePee and Kibo (the most recent to this reply to you) show you how it&#8217;s done.  In those cases, I suggested the use of an array, but for two files you could just as easily do it like this.  Assuming your linkage identifiers are &#8220;songA&#8221; and &#8220;songB&#8221;:</p>
<pre><code>var audio:Sound = new Sound();
audio.attachSound(&quot;songA&quot;);
audio.start();

audio.onSoundComplete = function():Void {
  this.attachSound(&quot;songB&quot;);
  this.start();
  delete this.onSoundComplete;
};</code></pre>
<p>The <code>onSoundComplete</code> handler is key:  when the first song ends, the next one is associated with the same <code>Sound</code> instance by way of the <code>attachSound()</code> method and is instructed to start playing &mdash; and the event handler is deleted so that, at the end of the second song, Flash doesn&#8217;t do anything else.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-138464</link>
		<pubDate>Sat, 23 Feb 2008 02:28:28 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-138464</guid>
					<description>Kibo,

Fortunately, there's not much to change.  :)  First, your array of sound files will be now be the Linkage identifiers of files in your Library, rather than external file names.  To add a Linkage identifier, right-click/Command-click your Library asset and choose Linkage.  Give the identifier field a unique name for each sound asset.  Then your array might look something like this:

&lt;pre&gt;&lt;code&gt;var listOfFiles:Array = new Array(&quot;frog&quot;, &quot;loon&quot;, &quot;horse&quot;);&lt;/code&gt;&lt;/pre&gt;

&amp;#8212; or whatever Linkage IDs you provided.

Then, wherever the original code says this:

&lt;pre&gt;&lt;code&gt;audio.loadSound(listOfFiles[currentFile], true);&lt;/code&gt;&lt;/pre&gt;

... you put this:

&lt;pre&gt;&lt;code&gt;audio.attachSound(listOfFiles[currentFile]);
audio.start();&lt;/code&gt;&lt;/pre&gt;

Check out my reply to GeePee (just before yours) to see a more detailed example of a similar situation.</description>
		<content:encoded><![CDATA[<p>Kibo,</p>
<p>Fortunately, there&#8217;s not much to change.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   First, your array of sound files will be now be the Linkage identifiers of files in your Library, rather than external file names.  To add a Linkage identifier, right-click/Command-click your Library asset and choose Linkage.  Give the identifier field a unique name for each sound asset.  Then your array might look something like this:</p>
<pre><code>var listOfFiles:Array = new Array("frog", "loon", "horse");</code></pre>
<p>&mdash; or whatever Linkage IDs you provided.</p>
<p>Then, wherever the original code says this:</p>
<pre><code>audio.loadSound(listOfFiles[currentFile], true);</code></pre>
<p>&#8230; you put this:</p>
<pre><code>audio.attachSound(listOfFiles[currentFile]);
audio.start();</code></pre>
<p>Check out my reply to GeePee (just before yours) to see a more detailed example of a similar situation.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Kibo</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-132514</link>
		<pubDate>Mon, 11 Feb 2008 22:53:08 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-132514</guid>
					<description>Dave,
thank you for helping us.I am new in ActionScript and your tutorial help me to better understand things that i learn in AC2&amp;#38;3.As you all know,learning script is hard without good exercise.This code is just what I was looking for.
I will be so daring to ask you help me one more basic thing.I tried to change this code to work with sounds from fla library rather than from URL's,but I failed.How can I do it?

thanx
Kibo</description>
		<content:encoded><![CDATA[<p>Dave,<br />
thank you for helping us.I am new in ActionScript and your tutorial help me to better understand things that i learn in AC2&amp;3.As you all know,learning script is hard without good exercise.This code is just what I was looking for.<br />
I will be so daring to ask you help me one more basic thing.I tried to change this code to work with sounds from fla library rather than from URL&#8217;s,but I failed.How can I do it?</p>
<p>thanx<br />
Kibo
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-120661</link>
		<pubDate>Mon, 14 Jan 2008 21:38:11 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-120661</guid>
					<description>GeePee,

I'm not sure what's going on in your &lt;code&gt;onSoundComplete&lt;/code&gt; handler.  The &lt;code&gt;Sound.duration&lt;/code&gt; property indicates how long an audio file is, which is determined by the composition of that file itself (you can't set &lt;code&gt;duration&lt;/code&gt;, but only retrieve it &amp;#8212; which isn't what you want to do here anyway).

Here's my take on a solution.  This is a custom function, &lt;code&gt;playSequence()&lt;/code&gt;, that receives an array parameter, spelling out the word &quot;dog&quot; as &quot;d&quot;, &quot;o&quot;, &quot;g&quot;.  It assumes those letters are the Linkage identifiers of three audio clips in the Library:

&lt;pre&gt;&lt;code&gt;function playSequence(sounds:Array):Void {
  var s:Sound = new Sound();
  if (sounds.length &gt; 0) {
    var currentSound:Number = 0;
    s.attachSound(sounds[currentSound]);
    s.start();
    s.onSoundComplete = function():Void {
      if (currentSound &lt; sounds.length - 1) {
        currentSound++;
        s.attachSound(sounds[currentSound]);
        s.start();
      }
    }
  }
}

playSequence([&quot;d&quot;, &quot;o&quot;, &quot;g&quot;]);&lt;/code&gt;&lt;/pre&gt;

In this way, it doesn't matter how many letters you pass in.  An arbitrarily named variable, &lt;code&gt;currentSound&lt;/code&gt; keeps track of which sound is currently playing (this is a numbered index of the array, &lt;code&gt;sounds&lt;/code&gt;).  As each comes to an end, the same &lt;code&gt;onSoundComplete&lt;/code&gt; handler executes &amp;#8212; notice, there's only one &lt;code&gt;Sound&lt;/code&gt; instance, and only one event handler &amp;#8212; until the array is run through completely.</description>
		<content:encoded><![CDATA[<p>GeePee,</p>
<p>I&#8217;m not sure what&#8217;s going on in your <code>onSoundComplete</code> handler.  The <code>Sound.duration</code> property indicates how long an audio file is, which is determined by the composition of that file itself (you can&#8217;t set <code>duration</code>, but only retrieve it &mdash; which isn&#8217;t what you want to do here anyway).</p>
<p>Here&#8217;s my take on a solution.  This is a custom function, <code>playSequence()</code>, that receives an array parameter, spelling out the word &#8220;dog&#8221; as &#8220;d&#8221;, &#8220;o&#8221;, &#8220;g&#8221;.  It assumes those letters are the Linkage identifiers of three audio clips in the Library:</p>
<pre><code>function playSequence(sounds:Array):Void {
  var s:Sound = new Sound();
  if (sounds.length > 0) {
    var currentSound:Number = 0;
    s.attachSound(sounds[currentSound]);
    s.start();
    s.onSoundComplete = function():Void {
      if (currentSound < sounds.length - 1) {
        currentSound++;
        s.attachSound(sounds[currentSound]);
        s.start();
      }
    }
  }
}

playSequence(["d", "o", "g"]);</code></pre>
<p>In this way, it doesn&#8217;t matter how many letters you pass in.  An arbitrarily named variable, <code>currentSound</code> keeps track of which sound is currently playing (this is a numbered index of the array, <code>sounds</code>).  As each comes to an end, the same <code>onSoundComplete</code> handler executes &mdash; notice, there&#8217;s only one <code>Sound</code> instance, and only one event handler &mdash; until the array is run through completely.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: GeePee</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-118701</link>
		<pubDate>Tue, 08 Jan 2008 17:46:15 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-118701</guid>
					<description>Hi David,

I think you are the best person which can solve my problem with this flash sound file.
I am having a text box and from that text box I want to hear sounds corresponding to the letters typed in the box. I dont know I had done perfect on this, but my code still working when I click to play it, but the problem is that it plays the whole sound files at the same time, but I want to play them one after another.... Please have a look at my code and correct it for me 

Here is my code:

&lt;pre&gt;&lt;code&gt;on (release) {
  var i, t, q;
  t=String(txt.text);
  for (i=0; i &lt;150; i++)   {
    q= mbsubstring(t,i,1);
      if (q == &quot;S&quot;)      {
        song1 = new Sound(); 
        song1.attachSound(&quot;S1&quot;);
        song1.onSoundComplete = function() { song1.duration(1000000) };
        song1.start();
      }
      else if (q == &quot;R&quot;)   {
        song2 = new Sound(); 
        song2.attachSound(&quot;R2&quot;);
        song2.onSoundComplete = function() { song2.duration(1000000) };
        song2.start();
      }
      else if (q == &quot;G&quot;)   {
        song3 = new Sound(); 
        song3.attachSound(&quot;G3&quot;);
        song3.onSoundComplete = function() { song3.duration(1000000) };
        song3.start();
      }
      else if (q == &quot;M&quot;)   {
        song4 = new Sound(); 
        song4.attachSound(&quot;M4&quot;);
        song4.onSoundComplete = function() { song4.duration(1) };
        song4.start();
      }
      else if (q == &quot; &quot;)   {
      gotoAndStop(1); 
    }
  }
}&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi David,</p>
<p>I think you are the best person which can solve my problem with this flash sound file.<br />
I am having a text box and from that text box I want to hear sounds corresponding to the letters typed in the box. I dont know I had done perfect on this, but my code still working when I click to play it, but the problem is that it plays the whole sound files at the same time, but I want to play them one after another&#8230;. Please have a look at my code and correct it for me </p>
<p>Here is my code:</p>
<pre><code>on (release) {
  var i, t, q;
  t=String(txt.text);
  for (i=0; i <150; i++)   {
    q= mbsubstring(t,i,1);
      if (q == "S")      {
        song1 = new Sound();
        song1.attachSound("S1");
        song1.onSoundComplete = function() { song1.duration(1000000) };
        song1.start();
      }
      else if (q == "R")   {
        song2 = new Sound();
        song2.attachSound("R2");
        song2.onSoundComplete = function() { song2.duration(1000000) };
        song2.start();
      }
      else if (q == "G")   {
        song3 = new Sound();
        song3.attachSound("G3");
        song3.onSoundComplete = function() { song3.duration(1000000) };
        song3.start();
      }
      else if (q == "M")   {
        song4 = new Sound();
        song4.attachSound("M4");
        song4.onSoundComplete = function() { song4.duration(1) };
        song4.start();
      }
      else if (q == " ")   {
      gotoAndStop(1);
    }
  }
}</code></pre>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-96009</link>
		<pubDate>Thu, 01 Nov 2007 18:32:27 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-96009</guid>
					<description>graeme,

Have fun with it!  Flash is &lt;em&gt;wired&lt;/em&gt; for multimedia, which is one of the main reasons I love it.</description>
		<content:encoded><![CDATA[<p>graeme,</p>
<p>Have fun with it!  Flash is <em>wired</em> for multimedia, which is one of the main reasons I love it.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: graeme</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-95869</link>
		<pubDate>Thu, 01 Nov 2007 01:37:44 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-95869</guid>
					<description>thanks again David..

looking forward to playing around with that....the notion of experimenting with artwork manipulated by sound loading and streaming also intrigues me and it feels as though this is a good place to start.

Graeme</description>
		<content:encoded><![CDATA[<p>thanks again David..</p>
<p>looking forward to playing around with that&#8230;.the notion of experimenting with artwork manipulated by sound loading and streaming also intrigues me and it feels as though this is a good place to start.</p>
<p>Graeme
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-93386</link>
		<pubDate>Wed, 24 Oct 2007 01:45:50 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-93386</guid>
					<description>graeme,

It's definitely possible to populate text fields with Sound info.  :)  If you check out the &lt;code&gt;TextField&lt;/code&gt; class in the ActionScript 2.0 (or 3.0) Language Reference, you'll find all kinds of information about text fields, of course.  One of the most basic features is the the &lt;code&gt;text&lt;/code&gt; property, which allows you to get or set the displayed text of any dynamic or input text field.  Properties are characteristics an object has, and most classes define public properties you can access.  Some are read-only, some not.

Here, you'll want to check out the &lt;code&gt;Sound&lt;/code&gt; class to see what properties it features (hint:  you'll find both &lt;code&gt;duration&lt;/code&gt; and &lt;code&gt;position&lt;/code&gt;, which should give you a good start).

Think of your goal as hooking together a home theater system.  You've got a DVD player (a ridiculous metaphor for the &lt;code&gt;Sound&lt;/code&gt; class) and a TV (an equally preposterous metaphor for the &lt;code&gt;TextField&lt;/code&gt; class).  Your text field's reference will be its instance name, as assigned via the Property inspector (this can also be done via ActionScript), and your sound's reference will be the variable that holds your &lt;code&gt;Sound&lt;/code&gt; instance.

&lt;code&gt;myTextField.text = mySound.position + &quot;/&quot; + mySound.duration;&lt;/code&gt;, etc.  :)</description>
		<content:encoded><![CDATA[<p>graeme,</p>
<p>It&#8217;s definitely possible to populate text fields with Sound info.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   If you check out the <code>TextField</code> class in the ActionScript 2.0 (or 3.0) Language Reference, you&#8217;ll find all kinds of information about text fields, of course.  One of the most basic features is the the <code>text</code> property, which allows you to get or set the displayed text of any dynamic or input text field.  Properties are characteristics an object has, and most classes define public properties you can access.  Some are read-only, some not.</p>
<p>Here, you&#8217;ll want to check out the <code>Sound</code> class to see what properties it features (hint:  you&#8217;ll find both <code>duration</code> and <code>position</code>, which should give you a good start).</p>
<p>Think of your goal as hooking together a home theater system.  You&#8217;ve got a DVD player (a ridiculous metaphor for the <code>Sound</code> class) and a TV (an equally preposterous metaphor for the <code>TextField</code> class).  Your text field&#8217;s reference will be its instance name, as assigned via the Property inspector (this can also be done via ActionScript), and your sound&#8217;s reference will be the variable that holds your <code>Sound</code> instance.</p>
<p><code>myTextField.text = mySound.position + "/" + mySound.duration;</code>, etc.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: graeme</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-92228</link>
		<pubDate>Sat, 20 Oct 2007 07:26:49 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-92228</guid>
					<description>Hi David.... I've drifted sideways into this sound blog and have been enjoying playing with everything talked about here but am trying to push ahead and get position and duration readings in dynamic text fields happening (or I suppose progress bars), first is it possible to achieve this for each file as it plays, my syntax knowledge is sketchy and I think the things I've tried are limited by this.....could you be so kind as to chuck me a clue if you get a spare moment.

thanks
Graeme</description>
		<content:encoded><![CDATA[<p>Hi David&#8230;. I&#8217;ve drifted sideways into this sound blog and have been enjoying playing with everything talked about here but am trying to push ahead and get position and duration readings in dynamic text fields happening (or I suppose progress bars), first is it possible to achieve this for each file as it plays, my syntax knowledge is sketchy and I think the things I&#8217;ve tried are limited by this&#8230;..could you be so kind as to chuck me a clue if you get a spare moment.</p>
<p>thanks<br />
Graeme
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-88164</link>
		<pubDate>Thu, 04 Oct 2007 21:03:21 +0000</pubDate>
		<guid>http://www.quip.net/blog/2007/flash/how-to-play-sound-files-sequentially#comment-88164</guid>
					<description>Peter,

Sounds like the ActionScript is doing what it should, but not in the way you expected.  Now that some time has passed, I can see why.  I should have notice this before.  ;)  The &lt;code&gt;onSoundComplete&lt;/code&gt; event handler is triggered at the &lt;em&gt;end&lt;/em&gt; of each song, which means the first song's name won't show.

In the lines that load the first song, add one additional bit of ActionScript to set the text field's &lt;code&gt;text&lt;/code&gt; property.  In fact, it's the same line used inside the event handler (the new line is added at the end):

&lt;pre&gt;&lt;code&gt;var currentFile:Number = 0;

var audio:Sound = new Sound();
audio.loadSound(listOfFiles[currentFile].file, true);
&lt;strong&gt;someTextField.text = listOfFiles[currentFile].name;&lt;/strong&gt;&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Peter,</p>
<p>Sounds like the ActionScript is doing what it should, but not in the way you expected.  Now that some time has passed, I can see why.  I should have notice this before.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />   The <code>onSoundComplete</code> event handler is triggered at the <em>end</em> of each song, which means the first song&#8217;s name won&#8217;t show.</p>
<p>In the lines that load the first song, add one additional bit of ActionScript to set the text field&#8217;s <code>text</code> property.  In fact, it&#8217;s the same line used inside the event handler (the new line is added at the end):</p>
<pre><code>var currentFile:Number = 0;

var audio:Sound = new Sound();
audio.loadSound(listOfFiles[currentFile].file, true);
<strong>someTextField.text = listOfFiles[currentFile].name;</strong></code></pre>
]]></content:encoded>
				</item>
</channel>
</rss>
