Search code examples
flash-cs4flash

How to edit mp3 file that was loaded to flash via fileReference.browse() method?


I wonder how can I work with mp3 that was loaded to to flash via fileReference.browse() method ?

Currently I've created an lash application that can load mp3 file from local hard drive to flash and provide an option to save mp3 file that was loaded back to the hard drive. But I can't find the way to work with mp3 that was loaded. I've heard about flash based editor that will be available in Aviary.(http://aviary.com/blog/posts/aviary-acquires-digimix), so there is a way to work with mp3 in flash, but how?

Here is my code:

import flash.net.FileReference;      
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.display.MovieClip;
import flash.net.*;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;



var fileReference:FileReference;
var myChannel:SoundChannel;
var mySound:Sound;
var mySprite:Sprite;
var mySprite2:Sprite;

mySound=new Sound();
myChannel=new SoundChannel();


load_btn.addEventListener (MouseEvent.CLICK, onLoadClick);
unload_btn.addEventListener (MouseEvent.CLICK, onUnloadClick);

function onLoadClick ( event:MouseEvent):void

{
    fileReference=new FileReference();
    var allTypeFilter:FileFilter = new FileFilter("mp3: (*.mp3)","*.mp3");
    fileReference.browse([allTypeFilter]);
    fileReference.addEventListener(Event.SELECT, selectHandler);

}

function selectHandler(event:Event):void
        {
            fileReference.removeEventListener(Event.SELECT, selectHandler);
            fileReference.addEventListener(Event.COMPLETE, loadCompleteHandler);
            fileReference.load();
        }

function loadCompleteHandler(event:Event):void
        {
            fileReference.removeEventListener(Event.COMPLETE, loadCompleteHandler);

            var loader:Loader = new Loader();
            //loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startPlay);
            loader.loadBytes(fileReference.data);
        }

function onUnloadClick (event:MouseEvent)

{
    trace(fileReference.data);
    fileReference.save(fileReference.data, "done.mp3");

}

Thanks in advance.


Solution

  • It's strangely complicated. For some reason, there isn't any API to create a sound object from either a FileReference or a ByteArray, so you have to parse (not decode) the MP3 and create a SWF that can be loaded via flash.display.Loader.loadBytes.

    Check out blog post for details and a library to do this. Once you've loaded the Sound object, you can use the extract() method to get the PCM audio data which you can use in your editor.

    To generate an MP3 from the edited PCM, it looks like there are some Flash mp3 encoders, but I haven't used any.