Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Done it! ( Well... almost :D )</p> <p>It was complicated but since my problem was only the audio of MovieClip I've created a class that acts like a mixer. </p> <p>The mixer is responsible to play all the sounds using a unique SampleDataEvent that mix the bytes of all my sounds. Also it generates a single sound data in ByteArray when i execute the startRecording funcion. Kind Complicated to explain but here is the code for the mixer:</p> <pre><code>/**** MySoundMixer initializations omitted ***/ //Generates the sound object to start the stream //for reproduce the mixed sounds. public function startStream() { fullStreamSound= new Sound(); fullStreamSoundData = new ByteArray(); this.fullStreamSoundChannel = this.fullStreamSound.play(); } //Adds a sound in the soundlib //(See: MySound object for more details) public function addSound(sound:MySound, key:String) { sound.initialize(); sounds.push({sound:sound, key:key}); } //Play a sound in the sound lib public function play(key) { var founded:MySound = null; for (var i = 0; i &lt; sounds.length; i++) { if (key == sounds[i].key) { founded = sounds[i].sound; break; } } if (founded != null) { founded.play(); } } // The SampleDataEvent function to Play the sound and // if recording is activated record the sound to fullStreamSoundData public function processSampleData(event:SampleDataEvent) { var pos = 0; var normValue:Number = 1 / this.sounds.length; while (pos &lt; BUFFER) { var leftChannel:Number = 0; var rightChannel:Number = 0; for (var i = 0; i &lt; this.sounds.length; i++) { var currentSound:MySound = this.sounds[i].sound; var result = currentSound.getSampleData(); leftChannel += result.leftChannel * normValue; rightChannel += result.rightChannel * normValue; } event.data.writeFloat(leftChannel); event.data.writeFloat(rightChannel); if (isRecording) { fullStreamSoundData.writeFloat(leftChannel); fullStreamSoundData.writeFloat(rightChannel); } pos++; } } //Starts recording public function startRecording() { this.isRecording = true; } //Stops recording public function stopRecording() { this.isRecording = false; } </code></pre> <p>The SampleDataEvent is used for play and extract the mixed sounds at the same time.</p> <p>I also had to create a MySound class that extends the Sound object in order to extract the sampleData of each sound (method <strong>getSampleData()</strong>) at current buffer used by <strong>processSampleData</strong> method. The MySound class also starts playing when Mixer starts (sending 0 bytes for both channels) and will stop also when the mixer stops, it will only start to send the byte information of the music when <strong>play()</strong> function is called.</p> <p>The class that i have created is something like this:</p> <pre><code>/**** MySound initializations omitted ***/ public function initialize() { this.extractInformation(null); } //Override the play function to avoid playing. //(The play act will be triggered by SoundMixer class) override public function play(startTime:Number = 0, loops:int = 0, sndTransform:SoundTransform = null):SoundChannel { this.isPlaying = true; this.currentPhase = 0; return null; } // On each buffer in sampledata i read the chunk of sound bytes public function getSampleData() { var leftChannel:Number = 0; var rightChannel:Number = 0; if (this.isPlaying) { if (currentPhase &lt; totalPhases) { this.soundData.position = currentPhase * 8; leftChannel = this.soundData.readFloat(); rightChannel = this.soundData.readFloat(); this.currentPhase ++; } else { stopPlaying(); } } return { leftChannel:leftChannel, rightChannel:rightChannel }; } //Extracts information of the sound object in order to //split it in several chunks of sounds. public function extractInformation(evt:Event) { trace("Inicializando o som " + this.id3); this.soundData = new ByteArray(); this.extract(soundData, int(this.length * SAMPLE_44HZ + 10000)); this.totalPhases = this.soundData.length / 8; this.currentPhase = 0; } ///Stop playing means stop extracting bytes public function stopPlaying() { this.isPlaying = false; } </code></pre> <p>With that, i have generated a unique ByteArray object that contains the hole sound information of the mixer. all i had to do is start the mixer when the movieclip starts and stop it also when the movieclip stops. The ByteArray information with the sound object is passed <strong>addFrame(bitmapData, sndData)</strong> of Alchemy Lib that records it successfully.</p> <p>It is working well in my project but i probably will need to optmize the code.</p> <p>Thanks all guys that helped me!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload