5 Replies Latest reply: Jan 15, 2012 12:35 AM by saariko RSS

    AIR3 ios app microphone recording/playing problem - beginning of recording is missing

    saariko Community Member

      hey

       

      i have build a small test app in Flash Pro 5.5 overlayed with the AIR 3 sdk.

      is has just 2 buttons to record and playback audio from the microphone.

       

      when i test this on my iPhone 3g - i record myself saying "1-2-3-4-5". but when i playback a half a second or so is missing from the beginning : "3-4-5-".

       

      when i test this on the desktop all is fine. also works fine on an android device.

      same problem when deploying on iPhone4

       

      if anyone could try this code and see if he/she reproduces this, i would appreciate it

      thanks

      Saar

       

       

       

       

      this is the app:

       

      all i have on stage is 2 rectangled movie clips named "recorded" and "player"

      all the code is in this document class:

       

       

      package  {

       

                import flash.display.MovieClip;

                import flash.media.Microphone;

                import flash.media.SoundMixer;

                import flash.events.MouseEvent;

                import flash.utils.ByteArray;

                import flash.media.Sound;

                import flash.media.SoundChannel;

                import flash.events.SampleDataEvent;

                import flash.media.AudioPlaybackMode;

       

       

                public class RecTest extends MovieClip {

                          var mic:Microphone;

                          var nowRecording:Boolean = false;

                          var nowPlaying:Boolean = false;

                          var recordedBytes:ByteArray = new ByteArray();

                          var s:Sound = new Sound();

                          var sc:SoundChannel;

       

                          public function RecTest() {

                                    mic = Microphone.getMicrophone(-1);

                                    SoundMixer.useSpeakerphoneForVoice = true;

                                    SoundMixer.audioPlaybackMode = AudioPlaybackMode.MEDIA;

                                    mic.gain = 100;

                                    mic.setSilenceLevel(0);

                                    mic.rate = 44;

       

                                    recorder.addEventListener(MouseEvent.CLICK, onRec);

                                    player.addEventListener(MouseEvent.CLICK, onPlay);

                          }

       

                          function onRec(e:MouseEvent) {

                                    if (nowRecording) {

                                              trace("stopped");

                                              mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);

                                              nowRecording = false;

                                    } else {

                                              trace("recording");

                                              recordedBytes.position = 0;

                                              recordedBytes.length = 0;

                                              mic.addEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);

                                              nowRecording = true;

                                    }

                          }

       

                          function onPlay(e:MouseEvent) {

                                    if (nowPlaying) {

                                              trace("stopped");

                                              sc.stop();

                                              s.removeEventListener(SampleDataEvent.SAMPLE_DATA, playAudio);

                                              nowPlaying = false;

                                    } else {

                                              trace("playing");

                                              recordedBytes.position = 0;

                                              s.addEventListener(SampleDataEvent.SAMPLE_DATA, playAudio);

                                              sc = s.play();

                                                      sc.addEventListener(Event.SOUND_COMPLETE, onComplete,false,0,true);

                                              nowPlaying = true;

                                    }

                          }

       

                              function onComplete(e:Event) {

                                                    trace("stopped");

                                                    s.removeEventListener(SampleDataEvent.SAMPLE_DATA, playAudio);

                                                    nowPlaying = false;

                          }

       

       

                          function getMicAudio(e:SampleDataEvent) {

                                    recordedBytes.writeBytes(e.data);

                          }

       

                          function playAudio(e:SampleDataEvent) {

                                    for (var i:int = 0; i < 8092 && recordedBytes.bytesAvailable > 0; i++) {

                                              e.data.writeBytes(recordedBytes);

                                              e.data.writeBytes(recordedBytes);

                                    }

                          }

                }

       

      }

       

      Message was edited by: saariko

       

      Message was edited by: saariko

        • 1. Re: AIR3 ios app microphone recording/playing problem - beginning of recording is missing
          saariko Community Member

          seems maybe it is partially ios's fault: built in "Voice Memos" app also begins a bit late

           

          workaround: i record audio constantly to a 2 second buffer and when the user hits "record" i record the in and out positions of the recordedBytes byteArray and use them for playback - i copy just the bytes i want to a playback byteArray :

          playBytes.writeBytes(recordedBytes, inPos, outPos);

          • 2. Re: AIR3 ios app microphone recording/playing problem - beginning of recording is missing
            Piyush kathayat Community Member

            Hi, Did you get the solution of your issue?? I am having same issue and not able to find out solution. Searched a lot but not able to understand. same recording application is working with Android device perfectlys. let me know if you have any olution for this issue.

             

             

            Thanks.

            • 3. Re: AIR3 ios app microphone recording/playing problem - beginning of recording is missing
              saariko Community Member

              if the only problem you have is recording starts late then the solution i have is the workaround i posted: record in infinite loop and use buttons to set in and out points, that way the beginning won't be missed

               

              other mic problems (studders and all) are fixed when deploying ios apps in "publish for itunes" method (=not quick publish)

              • 4. Re: AIR3 ios app microphone recording/playing problem - beginning of recording is missing
                Piyush kathayat Community Member

                could you please explain the soulution in detail. How did you record the in and out positions of the recordedBytes? can I have your sample code to see the solution.

                 

                seems your approach is the solution for the issue.

                • 5. Re: AIR3 ios app microphone recording/playing problem - beginning of recording is missing
                  saariko Community Member

                  hey,

                   

                  the idea is to use the record and stop buttons to set position and clear the bytearray into which the audio is being written, not to add and remove the eventlistener for SampleDataEvent itself

                   

                  ok first of all you need to be recording all the time into a buffer. in my case recording starts as you enter a specific page in the app and stops as you leave it, but you can have it running as long as your app is activated (remember to stop the recording and timers when the app goes to the background and deactivated).

                   

                  var curRecBytes:ByteArray = new ByteArray();

                  var playBackBytes:ByteArray = new ByteArray();

                   

                   

                  mic.addEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);

                   

                  function getMicAudio(e:SampleDataEvent) {

                      curRecBytes.writeBytes(e.data);

                  }

                   

                  you need a buffer timer that will clear the byteArray if the record button has not been pressed. mine is 2 seconds long, you can set it otherwise. nowRecording is a Boolean set to true when the record button is pressed and to false when the stop button is pressed

                   

                   

                   

                  micBufferTimer = new Timer(2000);

                  micBufferTimer.addEventListener(TimerEvent.TIMER, resetRecordedBytes);

                  micBufferTimer.start();

                   

                  function resetRecordedBytes(e:TimerEvent) {

                      if (!nowRecording) curRecBytes.clear();

                  }

                   

                   

                  when the record button is pressed do this

                   

                  nowRecording = true;

                  curRecBytes.clear();

                   

                  and when the stop button is pressed

                   

                  playBackBytes.clear();

                  curRecBytes.position = 0;

                  playBackBytes.writeBytes(curRecBytes);

                  nowRecording = false;

                   

                   

                  for playing back you play the playBackBytes