DriftingSteps
54 posts
|
I used the following script (AS3) I found online to play the music in my flash. I know very little about actionscript.
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
//Load sound using URLRequest
soundClip.load(new URLRequest("music.mp3"));
//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
function onComplete(evt:Event):void {
//Play loaded sound
sndChannel = soundClip.play();
isPlaying = true;
}
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}
As you can see in the script above, music_play and music_stop are instance names for the play and stop buttons. The .mp3 file is an external file. I managed to get the music playing locally on a browser, but there seems to be some problem online, as I couldn’t hear it when I uploaded it on the website (http://ulfhc.com.au/). Also, is it possible to load the .mp3 before the flash animation loads (maybe by using a simple preloader)?
|
|
|
NineFiveThree
1370 posts
|
The website you linked to throws an #2044 Error:
Error #2032: Stream-Error.
at ulfhc_v_fla::music_controller_60/frame1()
|
|
|
DriftingSteps
54 posts
|
Yes, I saw that now when I pressed the buttons. How do I get rid of that?
|
|
|
EndlessSporadic
10478 posts
|
There was a developer who made a game based on levels generated to local music. Perhaps you can contact them? I don’t remember the game or the developer, but if you played the game you might know of it.
|
|
|
UnknownGuardian
8130 posts
|
|
|
|
NineFiveThree
1370 posts
|
Originally posted by DriftingSteps:
Yes, I saw that now when I pressed the buttons. How do I get rid of that?
Your problem is the resolution of the relative url.
This is actually different from testing locally:
local: relative to the .swf file
online: relative to the html file embedding the .swf
There are lot’s of tools that can monitor network traffic.
One of them being firebug as plugin for firefox get one now!
It’s easy to find out what’s wrong with your requests now:
- start firebug
- open your website
- click the network tab, see what was going on:
You try to get this file: http://ulfhc.com.au/music.mp3
Which obviously does not exist, both your .swf and the .mp3 file are within a folder “flash”.
Solution:
Keep the .swf (.fla) and the .html that embeds it in the same directory.
While it is a solution to contact other devs, please use your own brain first.
Especially because you are probably not the first guy who has this problem.
btw. add your listener before you invoke load(), not afterwards.
|
|
|
EndlessSporadic
10478 posts
|
Originally posted by UnknownGuardian:
This guy?
It’s possible. I vaguely remember the game.
|
|
|
JamesObscura
250 posts
|
@NineThreeFive:
Don’t tell people not to ask questions. Seriously? Little childish don’t you think? If you’re not going to be helpful don’t post here. I’d rather people like OP post than people like you.
|
|
|
NineFiveThree
1370 posts
|
Originally posted by JamesObscura:
Don’t tell people not to ask questions.
That’s not what I said.
Originally posted by JamesObscura:
Little childish don’t you think?
Quite the opposite actually.
Originally posted by JamesObscura: If you’re not going to be helpful don’t post here.
Then why did you post?
|
|
|
DriftingSteps
54 posts
|
Hi NineFiveThree, thank you very much for your help. I gave the full URL to the music and it plays now. But only if I press ‘stop’ and ‘play’. Otherwise, the music doesn’t play. The client wants the music to play the moment the flash loads.
Would you be kind enough to help me with this?
Thank you again =)
|
|
|
Ace_Blue
1066 posts
|
Originally posted by JamesObscura:
@NineThreeFive:
Don’t tell people not to ask questions. Seriously? Little childish don’t you think? If you’re not going to be helpful don’t post here. I’d rather people like OP post than people like you.
WTH? The only thing 953 said in this thread that could remotely be construed as “do not ask questions” is this: “While it is a solution to contact other devs, please use your own brain first.” I agree with 953.
Unless you are saying that it’s fine to contact you personally, by any means necessary, anytime anyone has a problem with anything, you are agreeing as well. If it is what you’re saying, please provide detailed contact information in your next post.
Also, trying to intimidate others into not posting is something you have just done, not him.
|
|
|
JamesObscura
250 posts
|
He asked a question on a forum made for asking questions. What exactly do you find two find so terrible about that? And exactly how did I intimidate him? By telling him I’d rather people ask questions, then tell other people not to?
953 assumed he didn’t “use his brain” for what reason again? Because it’s so simple to the super programmer that is 953? Still seems pretty childish to me. URLs are hard for a lot of people. Local vs relative URLs are a relatively advanced topic.
I can’t believe you guys are actually chastising this guy for asking a question. What a joke.
Troll harder kids.
|
|
|
NineFiveThree
1370 posts
|
Originally posted by DriftingSteps:
But only if I press ‘stop’ and ‘play’. Otherwise, the music doesn’t play.
When I open the website, the music starts playing right away.
I assume the problem is your internet connection.
You start playing the sound only when it has finished loading. Your .mp3 file is 4MB in size, which may take a while to download entirely.
You can play the music right after you called load(). It should be buffered and played as it downloads.
(Think youtube: you can buffer the video and watch it while it hasn’t finished loading)
You can set additional setting in the second parameter of the load() function.
check the docs please
@James.
It seems like you’re more interested in the personal argument, which I’m not.
I’m not chastising anybody.
Please take the time to read my post again and think whom and what I was referring to.
The closest to trolling in this thread, is your behavior.
|
|
|
DriftingSteps
54 posts
|
Oh, that’s a relief. Thanks again for the help. And I’ll check the doc on load(). I don’t have AS3 knowledge so I find it a bit difficult when I put in scripts like this.
I guess I’ll label this question as solved.
|
|
|
ErlendHL
1311 posts
|
One thing; I think you should play the song while it’s loading rather than after (I’m not sure how to do this but someone here must know).
I love the song, what is it? :)
James and 953 I agree with you both in certain areas. Now peace.
|
|
|
NineFiveThree
1370 posts
|
Originally posted by ErlendHL:
One thing; I think you should play the song while it’s loading rather than after
start the Sound without waiting for it to finish loading, add context; just read my post
|
|
|
ErlendHL
1311 posts
|
Originally posted by NineFiveThree:
Originally posted by ErlendHL:
One thing; I think you should play the song while it’s loading rather than after
start the Sound without waiting for it to finish loading, add context; just read my post
So he would just do like this?
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
//Load sound using URLRequest
soundClip.load(new URLRequest("music.mp3"))
soundClip.play();
|
|
|
NineFiveThree
1370 posts
|
Originally posted by ErlendHL:
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
That’s not how you get the SoundChannel.
Please see Drifter’s code.
Originally posted by ErlendHL:
//Load sound using URLRequest
soundClip.load(new URLRequest(“music.mp3”))
soundClip.play();
I assume it will default to the default buffer time, if the context is null, so this should play the sound.
|
|
|
ErlendHL
1311 posts
|
Originally posted by NineFiveThree:
Originally posted by ErlendHL:
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
That’s not how you get the SoundChannel.
Please see Drifter’s code.
What even? I copied his code.
|
|
|
NineFiveThree
1370 posts
|
Originally posted by ErlendHL:
Originally posted by NineFiveThree:
Originally posted by ErlendHL:
//Create a new SoundChannel Object
var sndChannel:SoundChannel = new SoundChannel();
That’s not how you get the SoundChannel.
Please see Drifter’s code.
What even? I copied his code.
Yes you did. I tried to say that the incomplete quoting broke the code.
DriftingSteps sets the sound channel again later in his code, but that’s not within your snippet, so I mentioned it.
|
|
|
ErlendHL
1311 posts
|
Oh I see. Correction: (and I added the other functions)
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = new Boolean();
var pausePosition:Number = new Number();
//Create an instance of the Sound class
var soundClip:Sound = new Sound();
//Load sound using URLRequest
soundClip.load(new URLRequest("music.mp3"));
var sndChannel:SoundChannel = soundClip.play();
function btnPressController(evt:MouseEvent):void
{
switch(isPlaying)
{
case true:
music_play.gotoAndStop(2);
pausePosition = sndChannel.position;
sndChannel.stop();
isPlaying = false;
break;
case false:
music_play.gotoAndStop(1);
sndChannel = soundClip.play(pausePosition);
isPlaying = true;
break;
}
}
function btnPressStop(evt:MouseEvent):void
{
pausePosition = 0;
sndChannel.stop();
music_play.gotoAndStop(2);
isPlaying = false;
}
|
|
|
NineFiveThree
1370 posts
|
I guess initializing the channel with an object that was not derived from a sound works similar to a null object pattern.
You can start the process by calling the same function (btnPressController()) without a need to check the channel to prevent a null pointer.
//untested
//imports the necessary as events
import flash.events.Event
import flash.events.MouseEvent;
var isPlaying:Boolean = false;
var soundChannel:SoundChannel = new SoundChannel();
//Create an instance of the Sound class
var sound:Sound = new Sound();
//Load sound using URLRequest
sound.load(new URLRequest("music.mp3"));
btnPressController(null);
function btnPressController(evt:MouseEvent):void
{
if (isPlaying)
{
music_play.gotoAndStop(2);
soundChannel.stop();
}
else
{
music_play.gotoAndStop(1);
soundChannel = sound.play(soundChannel.position);
}
isPlaying = !isPlaying;
}
function btnPressStop(evt:MouseEvent):void
{
soundChannel = new SoundChannel();
music_play.gotoAndStop(2);
isPlaying = false;
}
One could go further and call btnPressStop to initialize the variables and states after declaring them.
|
|
|
DriftingSteps
54 posts
|
Hi, thanks a lot for all your help. Your code, 953, works perfectly, and the music loads is the way it should. I did add the following code to get the buttons working after btnPressController(null);
music_play.addEventListener(MouseEvent.MOUSE_DOWN, btnPressController, false, 0, true);
music_stop.addEventListener(MouseEvent.MOUSE_DOWN, btnPressStop, false, 0, true);
The only thing that doesn’t seem to work accordingly is the play/pause button. The music doesn’t pause, that is. It works like the stop button, as you can see in the updated website. Is it possibly because of the missing pausePosition?
EDIT - Okay, the flash isn’t working online… I’ll see what I can do…
|
|
|
NineFiveThree
1370 posts
|
No, it should work without pausePosition.
It’s similar to the example found in the docs
(There is a variable with that name in the example, but it’s just a local one)
|
|
|
DriftingSteps
54 posts
|
Okay, I’ll put your script only and upload the flash and see if it works.
EDIT - Well the flash works perfectly, the only problem is the play/pause button. It doesn’t pause the music, the button stops it and loads it from the beginning.
|