Can i record audio using Phonegap

2,039 views
Skip to first unread message

Hareem Ul Haque

unread,
Oct 29, 2010, 9:02:06 PM10/29/10
to phonegap
Is it possible to use Phonegap to record audio using the device's
microphone.

And is it possible to sync the local contacts list with Phonegap app.


Are the above possible for the Android platform

Regards
Hareem Haque

Bryce Curtis

unread,
Oct 29, 2010, 10:04:34 PM10/29/10
to phonegap
Hareem,

Here's an example of audio recording:

//-------------------------------------------------------------------------
// Audio recorder
//-------------------------------------------------------------------------
var mediaRec = null;
var recTime = 0;

/**
* Record audio
*/
function recordAudio() {
console.log("recordAudio()");
console.log(" -- media="+mediaRec);
if (mediaRec == null) {

var src = "myrecording.mp3";
mediaRec = new Media(src,
function() {
console.log("recordAudio():Audio Success");
},
function(err) {
console.log("recordAudio():Audio Error: "+err);
setAudioStatus("Error: " + err);
},
function(status) {
console.log("recordAudio():Audio Status: "+status);
setAudioStatus(Media.MEDIA_MSG[status]);
}
);
}

navigator.notification.beep(1);

// Record audio
mediaRec.startRecord();

// Stop recording after 10 sec
recTime = 0;
var recInterval = setInterval(function() {
recTime = recTime + 1;
setAudioPosition(recTime+" sec");
if (recTime >= 10) {
clearInterval(recInterval);
mediaRec.stopRecord();
console.log("recordAudio(): stop");
navigator.notification.beep(1);
}
}, 1000);
}

You can retrieve the local contacts using:

function getContacts() {
obj = new ContactFindOptions();
obj.filter = "";
obj.multiple = true;
obj.limit = 1000;
navigator.service.contacts.find(
["displayName", "name", "phoneNumbers", "emails", "urls", "note"],
function(contacts) {
var s = "";
if (contacts.length == 0) {
s = "No contacts found";
}
else {
s = "Number of contacts: "+contacts.length+"<br><table
width='100%'><tr><th>Name</th><td>Phone</td><td>Email</td></tr>";
for (var i=0; i<contacts.length; i++) {
var contact = contacts[i];
s = s + "<tr><td>" + contact.name.formatted + "</td><td>";
if (contact.phoneNumbers.length > 0) {
s = s + contact.phoneNumbers[0].value;
}
s = s + "</td><td>"
if (contact.emails.length > 0) {
s = s + contact.emails[0].value;
}
s = s + "</td></tr>";
}
s = s + "</table>";
}
document.getElementById('contacts_results').innerHTML = s;
},
function(e) {
document.getElementById('contacts_results').innerHTML = "Error:
"+e;
},
obj);
};

Currently, saving contacts is not supported.

Documentation is available for these features at http://docs.phonegap.com.

Regards,
Bryce

Hareem Ul Haque

unread,
Nov 2, 2010, 9:14:07 PM11/2/10
to phonegap
Thanks for the info. The code example you showed me. Is it possible to
do so on Android or its only for Iphone.

Regards
Hareem Haque

> Documentation is available for these features athttp://docs.phonegap.com.
>
> Regards,
> Bryce

Simon MacDonald

unread,
Nov 2, 2010, 10:18:31 PM11/2/10
to phonegap
It is possible to do on Android.

Simon Mac Donald
http://hi.im/simonmacdonald

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

Hareem Ul Haque

unread,
Nov 2, 2010, 11:48:43 PM11/2/10
to phonegap
Thanks for the heads.. up... can i install phonegap on my windows
machine to start development.

Regards
Hareem

On Nov 2, 10:18 pm, Simon MacDonald <simon.macdon...@gmail.com> wrote:
> It is possible to do on Android.
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald

Simon MacDonald

unread,
Nov 3, 2010, 8:57:12 AM11/3/10
to phonegap

ericburnley

unread,
Dec 13, 2010, 7:02:26 PM12/13/10
to phonegap
I do have an audio record/playback example working on my Android phone
(Droid Incredible, OS 2.2).

Now, I know Android's audio file format is not mp3 (though I did
export it to my PC to see if it'd play since the example in the
Phonegap docs called it an .mp3 extension). I recall reading
somewhere what the audio file format is, but can't seem to find it
now.

What file type is the audio that is captured by this recording
method? And what's the file type recorded from iOS?

I'm looking to save these files to a server for review, so they'd be
coming from different devices and I'm just trying to figure out what
sort of review toolset we'd need.

Simon MacDonald

unread,
Dec 14, 2010, 9:54:35 AM12/14/10
to phonegap
This is the code that setups up the Audio recording on Android from
com.phonegap.AudioPlayer.java.

this.recorder = new MediaRecorder();
this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
// THREE_GPP);
this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //AMR_NB);

And if you look up the support audio codecs on the Android Developer site:

http://developer.android.com/guide/appendix/media-formats.html

I don't know what we are using for iPhone.

ericburnley

unread,
Dec 14, 2010, 12:40:41 PM12/14/10
to phonegap
So am I correct in thinking I could alter the source code for those
lines to specify mp3 format as the output format / audio encoder?

ericburnley

unread,
Dec 14, 2010, 12:44:55 PM12/14/10
to phonegap
duh, i didn't read that chart correctly.

I see the encoder does not support MP3 as a file type. Thanks.

Guess I'll look into 3GP -> MP3/WAV file conversion next... if
anyone's done that research already, links/ideas on that would be
appreciated.

Simon MacDonald

unread,
Jul 10, 2012, 4:30:17 PM7/10/12
to phon...@googlegroups.com
yes

Simon Mac Donald
http://hi.im/simonmacdonald


On Tue, Jul 10, 2012 at 8:02 AM, dhiren patel <patel.d...@gmail.com> wrote:
> I can see in the JAVA code that audio file is encoding using following code
>
> this.audioFile = file;
> this.recorder = new MediaRecorder();
> this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
>
> this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //
> THREE_GPP);
>
> this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
> //AMR_NB);
>
> Is that mean that they are "audio/amr" format with extension as .3gp?
> -- You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>
> To compile in the cloud, check out build.phonegap.com
Reply all
Reply to author
Forward
0 new messages