2

I am somewhat new to AS but still trying to learn and accomplish what seems like tough stuff. I will prevail!

The code below shows my progress so far. I have a UI Loader that has a series of thumbnails. With each click of the thumbnail a new SWF file loads... well I'm trying to get an external series of mp3 files to do the same through an XML file. So far, I've managed to just get one audio file to play with every click AND it play over the existing file.

I simply want to get a new one to play with each thumbnail AND stop the previous audio file from playing if/when the user clicks a new thumbnail.

Please assist if possible...help me see the errors of my way. A little overwhelmed.

var imagesXML:XML;
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.addEventlistener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("lessons/images/Images5.xml"));


/////////////////////////////////////////////////////////////
////////////    thumbnail loader    /////////////////////////

var mySound:Sound;
var myChannel:SoundChannel;


function xmlLoaded(evt:Event):void
{
imagesXML = new XML(xmlloader.data);
var thumbLoader:UILoader;
for(var i:uint = 0; i < imagesXML.image.length(); i++)
    {
        thumbLoader UILoader(getChildByName("thumb" + 1));
        thumbLoader.load(new URLRequest("lessons/images/thumbs/" + imagesXML.image[i].@file));
        thumbLoader.buttonmode = true;
        thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
        var fullPath:String = "lessons/images/file-1.swf";
        mainLoader.load(new URLRequest(fullpath));
    }
}



/////////////////////////////////////////////////////////////
////////////////   load Images  /////////////////////////////

function thumbClicked(evt:MouseEvent):void
{
    var thumbName:String = evt.currentTarget.name;
    var thumbIndex:uint = uint(thumbName.substr(5));
    var fullPath:String = "lessons/images/" + imagesXML.image[thumbIndex].@file;
    mainLoader.load(new URLRequest(fullPath));

//stop any sound playing
    if (myChannel != null)
    {
        myChannel.stop();
    }

// load mp3 file
    mySound = new Sound(); myChannel = new SoundChannel();
    mySound.load(new URLRequest ("lessons/audio/narration/ch1p3.mp3"));
    mySound.play();
}

EDIT

I also need to make the sound that plays dynamic based off the thumbnail that was clicked.

This is my updated code:

var imagesXML:XML;
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.addEventlistener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("lessons/images/Images5.xml"));

var thumbSounds:Dictionary = new Dictionary();


/////////////////////////////////////////////////////////////
////////////    thumbnail loader    /////////////////////////

var mySound:Sound;
var myChannel:SoundChannel;


function xmlLoaded(evt:Event):void
{
imagesXML = new XML(xmlloader.data);
var thumbLoader:UILoader;
for(var i:uint = 0; i < imagesXML.image.length(); i++)
    {
        thumbLoader UILoader(getChildByName("thumb" + 1));
        thumbLoader.load(new URLRequest("lessons/images/thumbs/" + imagesXML.image[i].@file));
        thumbLoader.buttonmode = true;
        thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
        var fullPath:String = "lessons/images/file-1.swf";
        mainLoader.load(new URLRequest(fullpath));
        thumbSounds[thumbLoader] = "lessons/audio/narration/sound/ch1" + i + ".mp3";

 //load mp3 file
       mySound = new Sound(); 
       myChannel = new SoundChannel();
       mySound.load(new URLRequest ("lessons/audio/narration/ch1p3.mp3"));
       myChannel = mySound.play();
    }
}



/////////////////////////////////////////////////////////////
////////////////   load Images  /////////////////////////////

function thumbClicked(evt:MouseEvent):void
{
    var thumbName:String = evt.currentTarget.name;
    var thumbIndex:uint = uint(thumbName.substr(5));
    var fullPath:String = "lessons/images/" + imagesXML.image[thumbIndex].@file;
    mainLoader.load(new URLRequest(fullPath));
//stop any sound playing
    if (myChannel != null)
       {
          trace("HOLD sound");
          SoundMixer.stopAll();
       }
    mySound = new Sound();
    myChannel = new SoundChannel();
    mySound.load(new URLRequest (thumbSounds[evt.currentTarget]));
    myChannel = mySound.play();

    trace("now playing" + thumbSounds[evt.currentTarget]);
}
P.T. Fenn
  • 55
  • 8

1 Answers1

4

All you are missing, is populating the myChannel var with the sound channel that is created and returned when you call play() on a Sound.

So, instead of doing:

myChannel = new SoundChannel();
//this creates an empty sound channel that has no relevance to anything you're doing
//so later when you do myChannel.stop(), you're just stopping this empty channel

Do this:

myChannel = mySound.play();
//play returns the relevant sound channel to what you've asked to play

So it should look like this:

mySound = new Sound();
mySound.load(new URLRequest ("lessons/audio/narration/ch1p3.mp3"));
myChannel = mySound.play();

Reading the documentation for the play function, you'll find the following:

Generates a new SoundChannel object to play back the sound. This method returns a SoundChannel object, which you access to stop the sound and to monitor volume.


EDT

To address your comments, you need to associate the audio file with each UILoader item somehow. You could do that by extending the UILoader class and adding a property, or you could use a dictionary, or you could parse the thumb path for the identifier.

I'll show you the dictionary approach as it's probably the easiest:

//at the top of your code with your other vars
var thumbSounds:Dictionary = new Dictionary();

Then when you create your UILoader items in that loop, do this:

thumbSounds[thumbLoader] = "lessons/audio/narration/ch1p" + i + ".mp3";
//I'm just guessing on your naming convention above, eg ch1p1.mp3, ch1p2.mp3 etc

Now, in your click handler where you play the sound, do this:

//in an event handler, the event's currentTarget is a reference to item that you added the listener to, in this case the thumb that was clicked.
mySound.load(new URLRequest (thumbSounds[evt.currentTarget]));
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • 1
    Oops.. I gave him that Sound code (in another question) but forgot about that `myChannel = mySound.play();` cos it was off the top of my head. Happens. – VC.One Nov 25 '15 at 02:07
  • Thank you, thank you... one other thing. How would I change the audio for each click through the lesson? – P.T. Fenn Nov 25 '15 at 10:31
  • ok. so this did work for stopping the previous sound. Thank you, thank you!! How would you suggest getting a _different audio lesson_ to play for **each click of a thumbnail**? – P.T. Fenn Nov 25 '15 at 13:08
  • Here is where I'm at with this in my attempt at mimicking the XML approach used for my images. Added at the top: `var soundXML:XML;` `var soundLoader:URLLoader = new URLLoader ();` `soundLoader.addEventListener(Event.COMPLETE, soundloaded);` `soundLoader.load(new URLRequest("lessons/sound/Sound2.xml"));` **After this portion below I added a new function for soundLoaded**: `soundXML = new XML(soundLoader.data);` `var SoundLoader:UILoader;` `for(var s:uint = 0; s > soundXML.sound.length(); s++) {` `soundLoader = UILoader(getChildByName("ch1-" + s));` **CONT'd** – P.T. Fenn Nov 25 '15 at 14:40
  • still in the _for_ loop: `soundLoader.load(new URLRequest("lessons/sound/Sound2.xml" + soundXML.sound[s].@file)); } }` **Getting these errors** Access of possibly property data through a reference with static type fl.container:UILoader **and** Access of undefined property s. I'm not actually sure why this is not cycling through via loop my audio files in this xml doc, file names: _ch1-0.mp3, ch1-1.mp3, ch1-2.mp3_ and _ch1-3.mp3_ – P.T. Fenn Nov 25 '15 at 14:46
  • if you would not mind one more item. I implemented your above changes using the dictionary. Perhaps it is improper placement on my part but, using my original post as a reference: at the top I've placed `var thumbSounds:Dictionary = new Dictionary();` then with the UILoader portion `thumbSounds[thumbLoader] = "lessons/audio/narration/ch1p" + thumbIndex + ".mp3";` the **AS** is unable to find the `thumbIndex` be referenced for the `MouseEvent` in the below Event Handler. _Any further idea's with my issue?_ **Again thank you for all your help and time. This is saving me** – P.T. Fenn Nov 30 '15 at 13:13
  • 1
    Append to your question the code you are currently using, or put it in pastebin and send a link. Then I can have a proper look. – BadFeelingAboutThis Nov 30 '15 at 16:58
  • ok. I edited the original post to show how i am currently structured. Is this what you had in mind? If so, the **thumbIndex** inside the **loop** is throwing an error. – P.T. Fenn Nov 30 '15 at 21:36
  • 1
    change `thumbIndex` to `i` – BadFeelingAboutThis Dec 01 '15 at 01:09
  • `thumbIndex` changed to `i` -- throwing: _TypeError: Error #1009: Can not access a property or method of a null object reference_-- @ the xmlLoad; @ the dispatchEventFunction(); @ the dispatchEvent and @ the URLLoader/uncompleted()... This is also preventing my images to load. Only the initial image shows. This project is not online nor will it be, so can not share a link. I do hope i'm not wearing your patience. This is a learning experience on my part and you really are saving me. Thank you in advance. Is there another platform/way that we should be discussing this? Also new to Stack too. – P.T. Fenn Dec 01 '15 at 17:57
  • 1
    Did you change anything else? Changing `thumbIndex` to `i` should not throw that error. Are you using the debugger so you get the exact line of the error? – BadFeelingAboutThis Dec 01 '15 at 18:09
  • I went back through line/line and here are my results. I have the same code as above. yes on the debug. With the line `thumbLoader UILoader(getChildByName("thumb" + 1));` with the **1** in the code breaks now and with an **i** the images and thumbnails work. With that being said, debug kicks an error that reads: _Error #1009 Cannot access a property or method of a null object reference------ @Lesson2.fla/bkgrdInfo_mc_24/thumbClicked()--------Cannot display source code at this location_ if I click a thumbnail for another image to display, debug locks up.. happened multiple times. – P.T. Fenn Dec 02 '15 at 13:32
  • 1
    You need to do: `mySound = new Sound();` prior to loading the sound. That is likely your issue – BadFeelingAboutThis Dec 02 '15 at 16:56
  • ;) me again... I added the `mySound = new Sound();` where you suggested, also edited into the original post. Something different happened this time. _Security Error: #2000: No active security context._... each time i clicked on a thumbnail. Are we getting closer? **Again, thank you for taking the time for me and making it your challenge.** – P.T. Fenn Dec 02 '15 at 19:05
  • 1
    Hmm, that's a trickier one. Make sure all your files (images) exist. Maybe trace out the `fullPath` var right before you load it, make sure it's correct. We're moving much beyond the scope of this question now though, and if you have significant other challenges, you should pose them as new questions (otherwise this one get's too muddled) – BadFeelingAboutThis Dec 02 '15 at 21:02
  • ok. Thank you for all your help! I do appreciate it. Possibly, I'll post a new question and reward for my new issue. – P.T. Fenn Dec 03 '15 at 11:04
  • If you would not mind entertaining one more thing. I found, after checking the XML link was correct, that it was not directed to the file ;) After this I added a trace `trace("sound playing");` and this **IS** in fact coming up with no errors, but no audio too (edited in original post). I would not be posting this if I did not think this was almost solved. Any thoughts? Thank you in advance. Please let me know how I can further your, already great reputation on Stack too? – P.T. Fenn Dec 03 '15 at 12:38
  • **Amazing turn of events!** the trace statements show that each xml file is playing (0,1,2,3...) GREAT NEWS and that they are playing. I added in the `myChannel = new SoundChannel();` too. But last and final issue is stopping the previous sound as the thumb is clicked. They all play over each other. – P.T. Fenn Dec 03 '15 at 14:00
  • problem solved the complete and working original code has been edited to reflect the fix. Thank you and you are amazing. – P.T. Fenn Dec 03 '15 at 15:40