Hi is there a way to export mp3 using AS3? As in layering them one on top the other? (Just like you would in an Audacoty project using lame). Thanks in advance for any help given.
CodeBoy101 -2 Junior Poster in Training
Dani AI
Generated
Short answer: yes — you can layer/mix audio in AS3, but you cannot silently write arbitrary files from a browser SWF. Typical, reliable options are (A) mix/collect raw PCM in the SWF and POST a WAV/PCM blob to a server that runs LAME/FFmpeg to produce MP3, or (B) encode MP3 in the client using a third‑party encoder (Alchemy/ANE ports exist) then let the user save the result. Note Flash Player requires user interaction to save a file (or else upload to server), and Flash is no longer supported in modern browsers — plan accordingly. (airsdk.dev)
How to mix in AS3 (practical notes): extract or capture audio as 32‑bit floats (SampleDataEvent / Sound.extract), convert samples to a common sample rate/channel layout, sum per-sample with per-track gains, clamp or normalize to avoid clipping, then write the mixed samples into a ByteArray you will encode. Example mixing loop (AS3-style pseudo-code):
function mixByteArrays(a:ByteArray, b:ByteArray, out:ByteArray,
gainA:Number=1, gainB:Number=1):void {
a.position = b.position = out.position = 0;
while (a.bytesAvailable >= 8 && b.bytesAvailable >= 8) {
var la:Number = a.readFloat(); var ra:Number = a.readFloat();
var lb:Number = b.readFloat(); var rb:Number = b.readFloat();
var l:Number = Math.max(-1, Math.min(1, la*gainA + lb*gainB));
var r:Number = Math.max(-1, Math.min(1, ra*gainA + rb*gainB));
out.writeFloat(l); out.writeFloat(r);
}
} This relies on the SampleDataEvent / Sound.extract flow. (doc.neolao.com)
Encoding options and tradeoffs: a client-side AS3 MP3 encoder exists (Shine MP3 Alchemy ports) and can be used to convert a WAV/PCM ByteArray to MP3 inside Flash, but performance and quality are limited compared with LAME; many developers instead upload WAV/PCM and run LAME or FFmpeg on the server for speed, quality and reliability. If you do client-side encoding, remember to trigger a user save (FileReference.save) or upload the binary to your server. (github.com)
If this topic is being read long after the original thread: building new web audio tools around Flash is not recommended. Use the Web Audio API to mix offline or in an AudioWorklet and encode with JS/WASM encoders (lamejs or libmp3lame builds) or send raw WAV to a server for FFmpeg/LAME conversion. That route gives better cross‑browser support and future‑proofing. (developer.mozilla.org)
Notes/troubleshooting: ensure all tracks share sample rate and channels before mixing; mix in 32‑bit float if possible, then convert to 16‑bit PCM for most encoders; test on target platforms because client CPU/time to encode varies widely.
JasonHippy 739 Practically a Master Poster
As far as I'm aware, for obvious security reasons there is no way to directly write any kind of file directly from flash, however it is possible to use fscommand (in AS2) or the ExternalInterface class (in AS3) to send data to an external server-side script, which in turn can process the data and save files on the server. Upon successful creation of a file, external scripts will usually pass a hyperlink back into the .swf which will enable the user to download a copy of the file they created.
I've seen some online flash apps which allow you to create images or text files which use this method, but I've not really seen any of the back-end code.
I've certainly never seen it used to export mp3's! However, that said I couldn't entirely rule it out, it may be possible! If it's possible send bitmap data from a swf and then convert/save it via an external script, then it's reasonable to assume that you could probably do the same with an mp3!
I used fscommand in AS2 several years ago to get some flash based e-learning content to communicate with a content management system (passing user data and scores in and out), but I've never tried doing it with anything like images or sounds, so I wouldn't know where to start there.
And I've not used AS3's ExternalInterface class yet either, but take a look at this link. This should get you onto the right track:
The article in the link is pretty straightforward and demonstrates how to set up communication between flash and javascript.
It's not going to instantly solve your problem, but it should at least serve as a starting point!
Also here is the official Adobe documentation for ExternalInterface:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2
Edited by JasonHippy because: n/a
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.