Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Error - "Cannot read property 'capture' of undefined" on Android

Trying to create a starter app with PhoneGap. I'm stuck on this line:

navigator.device.capture.captureAudio(onSuccess, onError, {limits:1, duration:5});

It works fine on iOS but throws this error on Android emulator:

Cannot read property 'capture' of undefined

Here's navigator object, at the time of error (notice there's no device property which is causing this error)

{
    "language": "en-US",
    "product": "Gecko",
    "mimeTypes": {
        "length": 0
    },
    "appVersion": "5.0 (Linux; U; Android 4.0.3; en-us; sdk Build/MR1) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
    "plugins": {
        "length": 0
    },
    "onLine": true,
    "connection": {
        "type": 0
    },
    "platform": "Linux armv7l",
    "vendor": "Google Inc.",
    "appCodeName": "Mozilla",
    "geolocation": {},
    "cookieEnabled": true,
    "appName": "Netscape",
    "productSub": "20030107",
    "userAgent": "Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; sdk Build/MR1) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
    "vendorSub": ""
}

And here's the PhoneGap object just after Phonegap.js has loaded (notice it says device: true)

{
    "commandQueue": [],
    "commandQueueFlushing": false,
    "_constructors": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
    ],
    "documentEventHandler": {},
    "windowEventHandler": {},
    "resources": {
        "base": true,
        "debugconsole": true,
        "position": true,
        "acceleration": true,
        "accelerometer": true,
        "battery": true,
        "camera": true,
        "device": true,
        "capture": true,
        "contact": true,
        "file": true,
        "filetransfer": true,
        "geolocation": true,
        "compass": true,
        "media": true,
        "notification": true,
        "orientation": true,
        "sms": true,
        "telephony": true,
        "network": true,
        "splashscreen": true
    },
    "available": false,
    "sessionKey": 0,
    "callbackId": 0,
    "callbacks": {},
    "callbackStatus": {
        "NO_RESULT": 0,
        "OK": 1,
        "CLASS_NOT_FOUND_EXCEPTION": 2,
        "ILLEGAL_ACCESS_EXCEPTION": 3,
        "INSTANTIATION_EXCEPTION": 4,
        "MALFORMED_URL_EXCEPTION": 5,
        "IO_EXCEPTION": 6,
        "INVALID_ACTION": 7,
        "JSON_EXCEPTION": 8,
        "ERROR": 9
    },
    "mediaObjects": {}
}

Any ideas how to get past this?

like image 495
Mrchief Avatar asked Nov 05 '22 08:11

Mrchief


1 Answers

Ok, this is the most douchy solution I ever stumbled upon!

The JS for Android is different than that for iOS. The files is named the same though - phonegap-x.x.x.js. No points for guessing it'd be different for every other mobile platform.

I understand that you need to have different JS for different platform, but how about mentioning that somewhere?

Once I dragged the JS from Android folder, everything fell into place.

Thanks to this question for the answer: deviceready won't fire in Phonegap 1.0.0 on Android

like image 98
Mrchief Avatar answered Nov 09 '22 13:11

Mrchief