• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

microphone record, playback in AIR html js desktop app

Explorer ,
Oct 19, 2014 Oct 19, 2014

Copy link to clipboard

Copied

hi,

I want to record, save and playback the microphone audio in an HTML/JS AIR desktop app.

I can't find any examples so my code is based on AS3.

But the app crashes almost right away when the SampleDataEvent handler function is called.

Should it be in a while loop sames as AS3?

$(function(){

    var mic;

    var soundBytes = new air.ByteArray();

  

    $("#btnRecord").click(function(){

        mic = air.Microphone.getMicrophone();

        mic.rate = 44;

        mic.addEventListener(air.SampleDataEvent.SAMPLE_DATA, sampleEvent);

    })

 

  

    function sampleEvent(e){

        while (e.data.bytesAvailable) {

            var sample = e.data.readFloat();

            soundBytes.writeFloat(sample);

        }

    }


    $("#btnStop").click(function(){

        mic.removeEventListener(air.SampleDataEvent.SAMPLE_DATA, sampleEvent);

        $("#txtActivity").val("");

    })

  

    $("#btnPlay").click(function(){;

        for (var i = 0; i < 8192 && soundBytes.bytesAvailable > 0; i++) {

            var sample = soundBytes.readFloat();

            e.data.writeFloat(sample);

            e.data.writeFloat(sample);

            //we write the data twice because there are 2 channels (stereo)

        }

    })

 

  

}

thanks,

Jeff.

TOPICS
Development

Views

280

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 20, 2014 Oct 20, 2014

Copy link to clipboard

Copied

LATEST

this works on my macbook. But on my Windows 8.1 machine the audio gets played back twice as slow as normal.

Not a lower pitch, just twice as slow. Any idea why this is?

$(function(){

    var s;

    var sc;

    var nowRecording = false;

    var nowPlaying = false;

    var recordedBytes = new air.ByteArray();

//   air.SoundMixer.audioPlaybackMode = window.runtime.flash.media.AudioPlaybackMode.VOICE

   

    var mic = air.Microphone.getMicrophone();

    mic.rate = 44;

    mic.gain = 70;

    mic.setSilenceLevel(0);

    $("#btnRecord").click(function(){

        if (!nowRecording) {

            air.trace("recording");

            recordedBytes.clear();

            mic.addEventListener(air.SampleDataEvent.SAMPLE_DATA, getMicAudio);

            nowRecording = true;

            $(this).val("Click me to stop")

        }

        else {

            air.trace("recording stopped");

            mic.removeEventListener(air.SampleDataEvent.SAMPLE_DATA, getMicAudio);

            nowRecording = false;

            $(this).val("Click me to record")

        }

    })

   

    function getMicAudio(e){

        recordedBytes.writeBytes(e.data);

    }

   

    $("#btnPlay").click(function(){

        if (!nowPlaying) {

            air.trace("playing");

            recordedBytes.position = 0;

  s = new air.Sound();

            s.addEventListener(air.SampleDataEvent.SAMPLE_DATA, playAudio);

  sc = new air.SoundChannel

            s.play();

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

            nowPlaying = true;

          //  $(this).val("Click me to stop playing")

        }

        else {

            sc.stop();

            stopPlayback();

        }

    })

   

    function stopPlayback(e){

        air.trace("playing stopped");

        s.removeEventListener(air.SampleDataEvent.SAMPLE_DATA, playAudio);

        nowPlaying = false;

    }

   

    function playAudio(e){

  if(!recordedBytes.bytesAvailable > 0)

  return;

        for (var i = 0; i < 8192; i++) {

            var sample = 0;

  if(recordedBytes.bytesAvailable > 0) sample = recordedBytes.readFloat()

            e.data.writeFloat(sample);

            e.data.writeFloat(sample);

        }

    }

   

});

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines