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.

Manos Ppd
  • 71
  • 4
  • how are you getting the 4501,923 are you sure thats the duration and not the length? – The_asMan Jul 25 '11 at 16:11
  • 4501,923 is the duration of the soundClip in milliseconds. Sound.length - "The length of the current sound in milliseconds." – Manos Ppd Jul 25 '11 at 16:19
  • 1
    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. – The_asMan Jul 25 '11 at 19:39
  • 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;` – Ian Jul 25 '11 at 20:13
  • 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). – frankhermes Jul 25 '11 at 20:14
  • 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 Hz – Manos Ppd Jul 26 '11 at 14:24
  • @Ian - 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); } } }` – momo Nov 07 '11 at 20:19

3 Answers3

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!

Joan Llenas
  • 341
  • 2
  • 5
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.

-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.

Luke Van In
  • 5,215
  • 2
  • 24
  • 45
  • 2
    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: http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html – Bakapii Aug 02 '11 at 09:07