Reputation: 151
I know for a fact that this will have a simple answer, but I can't seem to wrap my head around it for the life of me...
I'm recording audio in my PhoneGap app using the default function found within the API documentation, and it works fine:
var audio_name = '1_' + Math.round(new Date().getTime()/1000) + '.mp3';
function recordAudio() {
var src = audio_name;
var mediaRec = new Media(src, onSuccess(src), onError);
// Record audio
mediaRec.startRecord();
// Stop recording after 10 sec
var recTime = 0;
var recInterval = setInterval(function() {
recTime = recTime + 1;
setAudioPosition("Recording Audio - " + recTime + " sec");
if (recTime >= 3) {
clearInterval(recInterval);
mediaRec.stopRecord();
setAudioPosition("Recording Saved As " + src);
}
}, 1000);
}
// onSuccess Callback
//
function onSuccess(src) {
console.log("recordAudio():Audio Success" + src);
}
// onError Callback
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
//
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
However, I've spent pretty much all day trying to figure out a way to manually halt the recording of the audio once its already started... any ideas?
Thanks in advance for the help!
Upvotes: 0
Views: 1138
Reputation: 7798
I've left only the needed methods. The only line you need to call is mediaRec.stopRecord();
and it will stop recording.
Assuming you want to stop it when the user hits a stop button (because you haven't specified when you want to halt it):
Set your divs:
<div id='startButton'>START</div>
<div id='stopButton'>STOP</div>
Assign the click event:
window.getElementById("startButton").onClick = recordAudio; //note you have no () here.
window.getElementById("stopButton").onClick = stopRecording; //note you have no () here.
This one starts your audio:
function recordAudio() {
var src = audio_name;
var mediaRec = new Media(src, onSuccess(src), onError);
// Record audio
mediaRec.startRecord();
}
This one stops it:
function stopRecording(){
//stop record
mediaRec.stopRecord();
}
Upvotes: 1