I haven't done this myself, but the way I would go about it follows this process:
- Load the MP3 file
- Decode the MP3 file
- Extract the bits you need
- Encode the extracted data
All of the below is to be considered untested pseudo code.
Step 1: Load the MP3 file
I assume that you already have a way to load the compressed MP3 file into memory. If not, there is plenty of information about this step to be found.
Step 2: decode the MP3 file
The Sound
class exposes the method loadCompressedDataFromByteArray
(docs) which takes the compressed MP3 file as a ByteArray
and decodes it to raw sound data.
Example:
var mySound:Sound = new Sound();
mySound.loadCompressedDataFromByteArray(myMP3Data, myMP3Data.length);
Step 3: Extract a part of the sound
Using the extract
(docs) method (as described in the link in your question) you can extract raw sound data from a Sound
object. You pass in a ByteArray
object to be populated with the data, specify where you want the "cut" to be, and the length of the clip to extract.
The data is stored using 44100 samples per second (left + right channel), and the calculations below is based on this. I might be wrong about this, so if it doesn't work as expected, please look this up further.
Example:
var sampleFrequency:int = 44100;
var startTimeInSeconds:Number = 30.0;
var lengthInSeconds:Number = 15.0;
// Calculate the position to start the clip (in samples)
var startPosition:Number = Math.round(startTimeInSeconds * sampleFrequency);
// Calculate the number of samples to extract
var samplesLength:Number = Math.round(lengthInSeconds * sampleFrequency);
var extractedBytes:ByteArray = new ByteArray();
mySound.extract(extractedBytes, samplesLength, startPosition);
Step 4: Encode the extracted sound back to MP3
Now that you have the sound data as a ByteArray, theres a few way to encode it back to the MP3 format. This previous StackOverflow answer mentions this library, but there might be other libraries out there better suited for your tasks.