Embed mp3 in actionscript 3 doesn't work
Asked Answered
M

3

7

I have a problem with actionscript 3, especially when using the embed tag. I have this code:

[Embed(source = "sound.mp3")]
private static var soundClip:Class;

...

var sound:Sound = (new soundClip()) as Sound;
trace(sound.length);

When I test the movie, I get the following output : 4501,923 Which is the sound.mp3 length in milliseconds. However, the original sound is 13.000 milliseconds.

How can that be possible, can anyone help me please?

Thank you.

Mick answered 25/7, 2011 at 15:13 Comment(7)
how are you getting the 4501,923 are you sure thats the duration and not the length?Mafia
4501,923 is the duration of the soundClip in milliseconds. Sound.length - "The length of the current sound in milliseconds."Mick
Are you sure bytesLoaded = bytesTotal at the time you are testing Sound.length. Depending on if the metadata was encoded at the end of the file or the beginning you might be getting false results.Mafia
One thing I noticed about your code above: if you have the embedded resource as a static var, you'll have to reference it through the class name, ie, var sound:Sound = new SoundAssets.soundclip() as Sound; The way you have it above, you would need your embedded symbol to lose the 'static' : private var soundClip:Class;Summand
What is the bitrate and sample rate of that mp3? Not all mp3's can be embedded like that (I've had problems with mp3's encoded at 24000KHz but they wouldn't compile).Rebarebah
I embeded several sounds. Most of the sounds can be played normally soundClip.play(); (whole duration), however, some of them have smaller duration. I guess that the problem is caused by the audio format. Sound's format is : mp3, bitrate: 128kbps, sample rate: 44100 HzMick
@Summand - that's how it should work, but not how it does work... You can access class members without prepending the class name in AS3: package { import flash.display.Sprite; public class Test extends Sprite { public static var s:String = "static"; public var i:String = "instance"; public function Test():void{ trace(s); trace(i); } } }Sandfly
S
1

The Embed process implies a transcodification (made by the compiler), if the sound doesn't fit into the supported Flash Player Sound format you can get unexpected results, like shrunk length. Check that the sounds you embed is at 44100 Hz Stereo, that is the supported sampling rate that won't cause any issues.

Cheers!

Spenserian answered 2/11, 2011 at 7:41 Comment(0)
K
0

your audio might have trouble being transcoded due to its sampling rate. a good standard is 44100Hz, but I've had 11025Hz work perfectly fine as well. you can easily change the sampling rate in seconds on nearly any audio file through Audacity, which is free and fairly powerful, but hardly intuitive.

Krys answered 3/4, 2016 at 10:42 Comment(0)
H
-3

I don't think you can load the MP3 just like that, you need to put it into a SWF first, then load the SWF, then instance the sound. Look here and here.

Hauteloire answered 30/7, 2011 at 22:11 Comment(1)
Actually you can load mp3s(and images) like that. If you try it in the Flash authoring tool, you will first get a pop-up informing you that you're using a Flex feature. That same pop-up will let you automatically update your Flex SDK path, then you're good to go. If you compile your SWFs via mlxmlc you're pretty much good to go right off the bat. See this link: livedocs.adobe.com/flex/3/html/help.html?content=embed_4.htmlDixie

© 2022 - 2024 — McMap. All rights reserved.