Skip to main content
Inspiring
October 3, 2011
Question

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

  • October 3, 2011
  • 5 replies
  • 2331 views

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

This topic has been closed for replies.

5 replies

saarikoAuthor
Inspiring
October 7, 2011

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);

Inspiring
January 13, 2012

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.

saarikoAuthor
Inspiring
January 14, 2012

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)