tailieunhanh - Học Actionscript 3.0 - p 36

Recording, Playing, and Saving Microphone Input 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class RecordMicrophone { private var _mic:Microphone; private var _sndBytes:ByteArray; private var _snd:Sound = new Sound(); private var _channel:SoundChannel = new SoundChannel(); public function RecordMicrophone() { setupMicrophone(); } public function setupMicrophone():void { _mic = (); = 44; (0); (true); } N OT E Note that the RecordMicrophone constructor does nothing but call the method setupMicrophone(). Why, then, isn’t the content of the latter method just placed inside the constructor? Remember that a constructor executes immediately upon instantiation but is not typically called again. By moving the setup routine to its. | Recording Playing and Saving Microphone Input 12 public class RecordMicrophone 13 14 private var _mic Microphone 15 private var _sndBytes ByteArray 16 private var _snd Sound new Sound 17 private var _channel SoundChannel new SoundChannel 18 19 public function RecordMicrophone 20 setupMicrophone 21 22 23 public function setupMicrophone void 24 _mic 25 44 26 0 27 true 28 Recording microphone input at runtime Lines 30 through 45 contain the code needed to start and stop recording. When the public startRecording method is called from a button we ll create later a new byte array is created to hold the new recording. Lines 33 and 34 add a SAMPLE_DATA event listener to the mic to prepare it for capturing input. First however line 32 checks to see if the listener doesn t already exist. This is a very useful technique to prevent the accidental creation of redundant listeners and makes it easier to remove listeners and manage memory and performance issues. NOTE Note that the RecordMicrophone constructor does nothing but call the method setupMicrophone . Why then isn t the content of the latter method just placed inside the constructor Remember that a constructor executes immediately upon instantiation but is not typically called again. By moving the setup routine to its own method you can setup your microphone any time you like. What s more by making the method public you can setup the mic from another part of your project such as a button or frame script if desired. The constructor calls this method too to enable recording the moment the RecordMicrophone class is instantiated. The getMicData method in lines 38 through 40 is the aforementioned listener method and does nothing more than write incoming bytes to the byte array. It s private because it s only accessed by the listener not from outside the class. The last method in this block stopRecording in lines 42 through 45 removes the .