-
as3, XML, and Sound
I am teaching myself as3 for my job through online tutorials. So far it has been going well, but I have hit a wall while dealing with playing different mp3 files that are defined in my XML. Currently in my XML I have some text, an image, and a sound file for each page. I am using a next and back button to go from page to page and change the content that is being displayed. I have gotten the images and text to work flawlessly but I am struggling with the audio. The first audio file is playing but when I hit the next button I get this error: "Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful." I am trying to get the audio file that is playing to change with the next and back buttons. I would greatly appreciate any help that someone could provide. Here is my as3 code so far:
Actionscript Code:
package { import flash.display.MovieClip; import flash.events.Event; import flash.events.ProgressEvent; import flash.media.Sound; import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.MouseEvent; import XMLList; import flash.display.Loader; public class Images extends MovieClip { public var xmlLoader:URLLoader = new URLLoader(); public var xmlData:XML = new XML(); public var index:int = 0; public var totalPages:int; public var pageNumber:int; public var imageLoader:Loader = new Loader(); public var snd:Sound = new Sound(); public function Images() { setupEvents(); } public function setupEvents():void { xmlLoader.addEventListener(Event.COMPLETE, loadXML); nextButton.addEventListener(MouseEvent.CLICK, nextPage); backButton.addEventListener(MouseEvent.CLICK, backPage); snd.addEventListener(Event.COMPLETE, soundPlay); xmlLoader.load (new URLRequest("data/images.xml")) } public function loadXML(e:Event):void { xmlData = new XML(e.target.data); totalPages = (xmlData.pic as XMLList).length(); trace(totalPages); gotoPage(index); } public function soundPlay (e:Event):void { snd.play(); } public function gotoPage(pageIndex:int):void { textBox.text = (xmlData.pic.text.text()[pageIndex]); headerTxt.text = (xmlData.pic.header.text()[pageIndex]); trace(xmlData.pic.image[pageIndex]); imageLoader.load(new URLRequest(xmlData.pic.image[pageIndex])); imageArea.addChild(imageLoader); snd.load(new URLRequest(xmlData.pic.audio[pageIndex])); } public function nextPage(e:MouseEvent):void { index ++; index = index % totalPages; gotoPage(index); } public function backPage(e:MouseEvent):void { index --; index = (index + totalPages) % totalPages; gotoPage(index); } } }
And here is my XML:
Code:
<images>
<pic>
<header>Kresge</header>
<text>This is an image with a building and some trees....Exciting!!!</text>
<image>images/image01.jpg</image>
<audio>audio/audio_01.mp3</audio>
</pic>
<pic>
<header>Media Lab</header>
<text>This looks tike the inside of a building but I am unsure what exactly is going on.</text>
<image>images/image02.jpg</image>
<audio>audio/audio_02.mp3</audio>
</pic>
<pic>
<header>Stata Center</header>
<text>Again, I have no idea what is going on in this image...but it showed up!!!</text>
<image>images/image03.jpg</image>
<audio>audio/audio_03.mp3</audio>
</pic>
<pic>
<header>Stata Lobby</header>
<text>This is the lobby of the Stata building.</text>
<image>images/image04.jpg</image>
<audio>audio/audio_04.mp3</audio>
</pic>
<pic>
<header>Construction</header>
<text>This is an image of a very large building being constructed. What will it be????</text>
<image>images/image05.jpg</image>
<audio>audio/audio_05.mp3</audio>
</pic>
<pic>
<header>Dome</header>
<text>This looks like an important building. Like I should know what it is...but I don't.</text>
<image>images/image06.jpg</image>
<audio>audio/audio_06.mp3</audio>
</pic>
<pic>
<header>Structure</header>
<text>Possibly an image of a building being built. Your guess is as good as mine.</text>
<image>images/image07.jpg</image>
<audio>audio/audio_07.mp3</audio>
</pic>
</images>
-
Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import XMLList;
import flash.display.Loader;
public class Images extends MovieClip {
public var xmlLoader:URLLoader = new URLLoader();
public var xmlData:XML = new XML();
public var index:int=0;
public var totalPages:int;
public var pageNumber:int;
public var imageLoader:Loader = new Loader();
public var snd:Sound = new Sound();
public var channel:SoundChannel = new SoundChannel();
public function Images() {
setupEvents();
}
public function setupEvents():void {
xmlLoader.addEventListener(Event.COMPLETE, loadXML);
nextButton.addEventListener(MouseEvent.CLICK, nextPage);
backButton.addEventListener(MouseEvent.CLICK, backPage);
xmlLoader.load(new URLRequest("images.xml"));
}
public function loadXML(e:Event):void {
xmlData=new XML(e.target.data);
totalPages = (xmlData.pic as XMLList).length();
trace(totalPages);
gotoPage(index);
}
public function gotoPage(pageIndex:int):void {
textBox.text = (xmlData.pic.text.text()[pageIndex]);
headerTxt.text = (xmlData.pic.header.text()[pageIndex]);
imageLoader.load(new URLRequest(xmlData.pic.image[pageIndex]));
imageArea.addChild(imageLoader);
trace(xmlData.pic.audio[pageIndex]);
channel.stop();
snd = new Sound();
snd.load(new URLRequest(xmlData.pic.audio[pageIndex]));
channel=snd.play();
}
public function nextPage(e:MouseEvent):void {
index++;
index=index%totalPages;
gotoPage(index);
}
public function backPage(e:MouseEvent):void {
index--;
index = (index + totalPages) % totalPages;
gotoPage(index);
}
}
}
-
Thank you so much. I knew I was close but couldn't find the problem.
-
Originally Posted by ballaby
Thank you so much. I knew I was close but couldn't find the problem.
can you share your fla?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|