I have a strange behavior when playing a recorded audio from the microphone on my ios device for the second time.
Goal:
what i want to achieve is an app where we can record multiple audio files with the buildin microphone, also it has to be possible to re-record an existing audio.
The troubles:
The first time i record an audio from the microphone and want to play it after finishing the record it works like a charm. Audio plays fine.
The second time i want to record an audio with the same byteaArray or a new byteArray, and want to play the recorded audio. The SampleDataEvent.SAMPLE_DATA is fired a few times but stops after 4-6 times and then does notting. The app does not crash, i dont get any errors. When i exit the app (press home button on the iPad) and after that open the app again the audio starts to play. SampleData is fired again and the app works fine. But also here if i record a new audio, it does not play. Exit app, open it again and it's playing.
Recording code (AudioRecord.as):
public function activate():void {
//
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.gain = 50;
mic.rate = 44;
}
public function startRecord():void {
trace("start audio record");
isRecording = true;
recordTime = 0;
//
soundBytes = null;
soundBytes = new ByteArray();
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
}
private function micSampleDataHandler(event:SampleDataEvent):void {
while (event.data.bytesAvailable) {
var sample:Number = event.data.readFloat();
soundBytes.writeFloat(sample);
}
}
Play audio code (AudioBytesPlayer.as):
public function playSound(bytes:ByteArray):void {
trace("AudioBytesPlayer - startSound");
isPlaying = true;
soundData = bytes;
soundData.position = 0;
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSC);
soundChannel = sound.play();
}
private function playbackSampleHandler(event:SampleDataEvent):void {
trace("playbackSampleHandler");
for (var i:int = 0; i < 8192; i++) {
if (soundData.bytesAvailable < 2) {
stopSound();
break;
}
var sample:Number = soundData.readFloat();
event.data.writeFloat(sample);
event.data.writeFloat(sample);
}
}
Hope you guys can help me out.