Phonegap: Recording audio with Phonegap

I was working on a Phonegap app recently and one of the goals was to allow the user to record some feedback as an audio file which would later be shared. If you hit Google, there’s plenty of StackOverflow threads which claim to have working sample code. Almost all of the SO code is using the Phonegap Media API with the “startRecording”/”stopRecording” functions. Despite it looking simple, I could never get the Media based code working on iOS. For whatever reason, I kept hitting iOS permission errors trying to create the recording file. On Android, the code works but I kept ending up with an “AMR” file instead of the expected 3gp recording.

So what’s the alternative? Turns out there’s another Phonegap API called Capture which has a captureAudio method which lets you record audio. As opposed to Media, captureAudio uses the device’s native audio recording UI to let you to record audio. Because of this, captureAudio won’t work for all use cases but the implementation is straightforward and consistent across Android and iOS.

Anyway, here’s the code to record some audio and upload it to a server:

Then, on the server you can just grab the file from say the $_FILES[“file”] variable.

Posted In: Javascript

Tags: ,

  • realwebdesigns

    would be great if you could share a demo of it thanks for the post

  • Thanks for the tip. Any idea if it is possible to record audio, while pressing a button, then stop when released, and again, following the previous record ? Like Vine do for video for example. Thanks !

  • Good call – will work on getting one together

  • So according to their docs there is. If you use the Media API (http://docs.phonegap.com/en/3.3.0/cordova_media_media.md.html#media.startRecord) it’ll let you build an interaction like that.

    We had problems getting it to work cross platform though.

  • realwebdesigns

    thanks

  • kbeaux

    Hi, why can’t i show the code ?
    there’s only that:

    Anyway, here’s the code to record some audio and upload it to a server:

    Then, on the server you can just grab the file from say the $_FILES[“file”] variable.

    Thx.

  • Brainium Infotech

    hi