UsifShahin UsifShahin - 16 days ago 6
Javascript Question

Javascript can't find these variables, why?

I'm using Phonegap to create an Android application (Voice Recorder), but I had these 2 errors in my code:


  1. ReferenceError: Can't find variable: Media.

  2. TypeError: Result of expression 'mediaRec' undefined is not an object.



The first error happens when the application runs. The second error happens when I call the recordAudio(); method.

Tell me what's the problem please if you know.

var mediaRec;
var src;
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {

}
function init() {
document.getElementById('status').innerHTML = "Recording Status";
src = "myrecording.mp3";
mediaRec = new Media(src, onSuccess, onError);
}

function recordAudio() {
// Record audio
mediaRec.startRecord();
// Stop recording after 10 sec
var recTime = 0;
var recInterval = setInterval(function() {
recTime = recTime + 1;
setAudioPosition(recTime + " sec");
if (recTime >= 10) {
clearInterval(recInterval);
}
}, 1000);
}

// Stop audio
function stopRecording() {
if (mediaRec) {
mediaRec.stopRecord();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// onSuccess Callback
function onSuccess() {
console.log("recordAudio():Audio Success");
}
// onError Callback
function onError(error) {
alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
}
// Set audio position
function setAudioPosition(position) {
document.getElementById('rec_position').innerHTML = position;
}


Thanks.

Answer

I think scr = "/android_asset/www/myrecording.mp3" because directory phonegap in asset/www/... If directory not found javascript in phonegap show alert like that ReferenceError: Can't find variable: Media.

Comments