Skip to main content
Inspiring
March 7, 2008
Question

Trouble Understanding variables

  • March 7, 2008
  • 29 replies
  • 2891 views
I am committed to learning A3, but HOLY CRAP!! I have never been a programmer, just a lowly designer. Apparently a pretty dumb designer! :)

I can breeze through this in AS2 with no problem, but AS3 is killin' me! I am trying to create a music player that will embed in a web page, it will be XML driven and the XML will be populated via a database and generated by PHP. I am using a listbox component to display the playlist (from the XMLfile). I have figured out how to start playing the first mp3 on the list.

Now when the user click a different button, I can get that song to play also, but I ended up created a second sound object, so the two play at the same time. Not good. I have tried and tried to access the object that is playing the first sound so I can stop it and load a new sound into it - I am about to pull my hair out!

I seem to missing a basic understanding of how to access this object and change the variable value so I can play a new song. Any help would be GREATLY appreciated!!
This topic has been closed for replies.

29 replies

Inspiring
March 26, 2008
Vern,

> When I place the files on MY server for testing, the download
> streams close every time. The exact same files on the clients
> server do not stop downloading.

Wow, that's interesting -- and good to know (thanks!). I've been
holding off on replying to your previous post because I didn't know the
answer and was waiting for some research time.

> Not yet solved, but at least we know where to look now.

That's how it gets done, often -- by hook or by crook. ;) Good job on
the troubleshooting!


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
March 26, 2008
David, and anyone else looking in concerning the close() method ...

Problem solved, or at least ID'd. The issue isn't in my Flash file. I have downloaded and installed a handy little took called Charles that allows me to monitor the communication between my browser and the server. When I place the files on MY server for testing, the download streams close every time. The exact same files on the clients server do not stop downloading. The PHP developer assumed the issues was in my Flash, as did I. It's not.

Not yet solved, but at least we know where to look now.

Thanks again for all the help!
Inspiring
March 25, 2008
Well, this is what I came up with:

function trackChange(butEvent:Event):void
{
myChannel.stop();
if(mySound.bytesLoaded < mySound.bytesTotal)
{
mySound.close();
}
nextClickedTrack();
}

If I don't use the if statement, it will throw an error telling me that it can't close a stream that doesn't exist. (if the download is complete)

So I get no errors, and I have included and IOErrorEvent listener in the above. THAT stopped the browser crash! But ... the download still doesn't stop. :( Since the crash is solved, I thought that would be good, but Flash wont let the user go to another page until all the download are complete. Seems odd, didn't know it would do that. I am not getting that issue on my PC with FF or IE. Again, seems to be a Mac and Safari issue.

In the nextClickedTrack function, I tried using:
mySound.load() instead of mySound = new Sound but I get #2037 telling me that the sequence is incorrect OR that an earlier call was unsuccessful. But that happens after the close() so I can't see how that would stop it from closing.

Yesterday, my hair was starting to grow back. Today, not so much. :)

-- Didn't see your last until I posted this -- I'll move that close() into the other function to keep code cleaner, but I don't think that will solve a problem. But I agree, good practice. :)
Participant
March 25, 2008
David,

I think I may need rudimentary XML guidance.

We need to step back a bit.

I have simplified my code, which I have attached.

When I load *that* code, I get the following output:

"music/Firefly.mp3"
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
at mp3Player_fla::MainTimeline/loadSong()
at MethodInfo-18()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

Allow me to attempt to interpret:

"music/Firefly.mp3" = the result of the trace statement.

However, there is a streaming error which is inhibiting the loadSong function from executing. I am guessing that it is trying to pull the location of the mp3 from the XML stream but cannot understand the content or structure. At this point, I am lost.

The line "music/Firefly.mp3" is the URL to the music file. I need help with the rest.

The structure of the mp3 file is simple:

<playlist>
<song>
<mp3>"music/Firefly.mp3"</mp3>

The trace statement results in an output of: "music/Firefly.mp3". If I plug that directly into the loadSong function, function:

loadSong(thisSong:String):void {
song = new Sound();
song.load(new URLRequest("music/Firefly.mp3"));

The song plays.

If I use the statement: loadSong(songList.song[0].mp3); -- it results in the above error, even though it yields in the same content through a trace statement = "music/Firefly.mp3". I can provide this identical code (cut and paste) to the song.load request. I would think the loadSong function would use this URL to load the song, but that is obviously not what is happening.

I have to confess -- While your mp3 player example works fine when the file locations are described in the Flash movie, I am totally baffled by the references to "thisSong". I have mimiced it, but don't know why you have it.

One last question related to the use of detailed XML content = if the ID3 content contains all the relevant information I need to describe the contents of the mp3, why should I bother with including that in the XML descriptions, which I would be using that to populate the list box. As I see it, the simplfied way of doing this is that the XML should contain only the mp3 location and I can query the file for the other ID3 content I want. What am I missing?

Terry
Inspiring
March 25, 2008
Vern,

To keep related items together, I would probably put the Sound.close()
call into the nextClickedTrack() function.

function trackChange(butEvent:Event):void {
isPlaying = false;
nextClickedTrack();
}

function nextClickedTrack():void {
currentTrackNum = listBox.selectedIndex;
mySound.close();
mySound = new Sound(new URLRequest(songs.track[currentTrackNum].url));
// etc.
}

> I am thinking, from what you said, that I need to change that
>'new Sound' call into a mySound.load instead. Set me straight
> if I am in the wrong direction, and in the mean time, I am
> headed that way! :)

Off the top of my head, I think mySound.load() should do it, but I
haven't tested for sure (and I normally do). Let me know how it goes! :)


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
March 24, 2008
Hi David,

Well, I may be close, not sure. You may have hit this ...

This is broken up into two functions because of the different options a user has. So, I have:
listBox.addEventListener(Event.CHANGE, trackChange);
function trackChange(butEvent:Event):void
{
mySound.close();
isPlaying = false;
nextClickedTrack();
}

and then:

function nextClickedTrack()
{
currentTrackNum = listBox.selectedIndex;
mySound = new Sound(new URLRequest(songs.track[currentTrackNum].url));
trackLabel.text = "Track : "+songs.track[currentTrackNum].title.text();
artistLabel.text = "Artist : "+songs.track[currentTrackNum].artist.text();
myChannel = mySound.play();
isPlaying = true;
myChannel.addEventListener(Event.SOUND_COMPLETE, nextAutoTrack);
}

I am thinking, from what you said, that I need to change that 'new Sound' call into a mySound.load instead. Set me straight if I am in the wrong direction, and in the mean time, I am headed that way! :)
Inspiring
March 24, 2008
Vern,

> So .. how can turn off - close -that download stream. I have
> tried myChannel.close() - but the problem is - the user then
> has to click the play button to get the player to go again.

That may be true with the code you've got in place, but remember, you're
in the driver's seat. :) If you need to use the close() method, which does
close the stream, causing any download of data to cease, then you need to
consider all the places in your code that load new MP3 files and update all
those places. This might just be in one place for you -- the custom
loadSong() function, if you ended up using code from Tom's and my book. But
either which way, I would think you'd only have to precede any call to
Sound.load() with Sound.close(). (The close() method belongs to Sound
class, just to be clear. It looks like you're referring to the SoundChannel
class, but I'm not sure.)

Does that make sense?


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
March 24, 2008
Hi David,

Since you are still checking in on this thead, can you point me in the right direction on this one? My player is complete, but if the user clicks on a second, or third, fourth etc, track, all the tracks continue to download. One of the team discovered this while watching server data transfer. This doesn't seem to be a big deal on my PC in either FF or IE. But on his Mac and Safari, he says it will crash the browser if he then tries to nav out of the page the player is on.

So .. how can turn off - close -that download stream. I have tried myChannel.close() - but the problem is - the user then has to click the play button to get the player to go again. Without the close() in there, the next track plays simply on a listBox click.

Someone suggested that I use removeEventListener - but didn't give any hint as to where, so I tried a few ideas that came up blank. Someone suggested that I couldn't shut off the download at all, but he wasn't sure in AS3.

Any ideas? :)
Inspiring
March 12, 2008
Vern,

> I REALLY appreciate the time you have taken.

Hey, it's good to hear you're making progress. That's what these forums
are for. :)

> Tomorrow - 15 CIGARS!! If you happen to like cigars, it
> would be a SMALL token of my appreciation to send you
> a couple of my favorites!

Ha! I do enjoy a cigar every now and again, but only when I'm far away
from my wife's nose, like at a conference. People do occasionally hit my
Amazon Wish List (I'm the one in Virgina Beach), but seriously, you
shouldn't feel obliged to send me anything. When I was getting started, I
received tons of help on these forums, so it all goes around and comes
around.

Continued good luck with this project, man. We covered quite a bit of
ground, and it sounds like you had some good, insightful moments. That sort
of thing builds on itself.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Participant
March 21, 2008
David,

I am using the MP3 player you have in your book as a reference for building one myself. It works fine when the mp3s locations are described in the AS3 code. But I can't get the player to recognize an mp3 described in an XML file. What I would like to do is get the player to recognize the XML right away, but I can't get that to work. In digging around on the web, people seem to convert the XML into an array and use that. That method does not work for me either.

Here is the code:

Here is the error I get:
TypeError: Error #2007: Parameter url must be non-null.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at mp3Player_fla::MainTimeline/loadSong()
at mp3Player_fla::MainTimeline/frame1()
songList: "music/Firefly.mp3","music/Nothing Without You.mp3","music/Lady of the Lake.mp3","music/Cult of Personality.mp3","music/Angel of Mine.mp3","music/No Regrets.mp3"

The trace statement picks up the elements now in the array, but if I run a trace statement after the event.Complete function, the array seems empty. I would rather not use the array, but I have no idea how to proceed.

Thanks in advance

Terry



Participant
March 21, 2008
David,

Just to complete my challenge, if I paste "playList" into the loadSong function, call, I get a streaming error. If I use "songMXL", I get: "TypeError: Error #1009: Cannot access a property or method of a null object reference".

My best guess as to what is happening is that songXML and songList (array) are empyting out after the function runs. playlist, which is the variable which captures the XML stream should be able to feed the loadSong function directly, shouldn't it?

Terry
Inspiring
March 12, 2008
David,

:)

I REALLY appreciate the time you have taken. I can't say enough! I will look into that chapter you sent the link to.

The good news is -- I found my problem with the volume and got it back. This thing is functional now!!!! (other than dragging the playhead) I just have to add the "speakers" with the spectrum analyzer.

Tomorrow - 15 CIGARS!! If you happen to like cigars, it would be a SMALL token of my appreciation to send you a couple of my favorites!

I will get the "speakers" working and then come back to finish up the playhead dragging feature.

THANXX!!!!!!!!!!!!!!!!!!!!!!

Inspiring
March 12, 2008
Vern,

I wonder if it would help you bet your bearings by looking at the sample
file for Chapter 5 of Foundation Flash CS3?

http://foundationflashcs3.com/sample-files.html

You'll find an MP3 player in there. All the code is in frame 1, neatly
laid out, and it features most of what you've been talking about -- for
example, a scrubber, volume control, songs that automatically launch the
next song, etc. Not the List component, but I think you have that part
covered.

> (it's probably something stupid, I am just about cross-eyed
> at this point!)

That may be part of it. Stress does that to me, too!

Part of the trouble, from my point of view, is that I don't have a clear
understanding of your full code, and you've obviously be changing as you go
(which is 100% understandable ... you're building a sculpture in progress).

But maybe if you look at the book sample code, it'll help you see how
all the parts fit together. By "all the parts," I obviously don't mean --
couldn't mean -- all the parts needed for your specific project ... but at
least something that works. It would maybe give you the sense of clarity
that comes with seeing a diagram ("a picture is worth a thousand words" sort
of thing).

I'm not trying to string you along, by the way ... my schedule gets
pretty wild sometimes (and flexible, which is both a good and bad thing!)
... so by coincidence, I don't have as much time to help tonight as I did
earlier this afternoon and yesterday.

Let me know if this helps.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."