0
votes

I am new to phonegap/cordova. This is my first App. My target is Android 5.1 or better. I'm using Cordova CLI.

I am building an app which uses the cordova media plugin. I installed the plugin and although I see all the required files declarations in config.xml, the javascript gives an error 'ReferenceError: Media is not defined' when I create the Media object. The changes I have done are as follows.

1). Installed media plugin using cordova CLI cordova plugin add org.apache.cordova.media

2). In platforms\android\res\xml\config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.HelloWorld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
    <preference name="loglevel" value="DEBUG" />
    <feature name="File">
        <param name="android-package" value="org.apache.cordova.file.FileUtils" />
        <param name="onload" value="true" />
    </feature>
    <feature name="Media">
        <param name="android-package" value="org.apache.cordova.media.AudioHandler" />
    </feature>

3). In platforms\android\AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

4). In index.html

<head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" media = "screen and (orientation: landscape)" href="css/index.css" type="text/css">
        <link rel="stylesheet" media = "screen and (orientation: portrait )" href="css/index_portrait.css" type="text/css">
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" src="cordova.js"></script>

5). In index.js, I create a media object on deviceready which gives the following error - ReferenceError: Media is not defined'

I checked the points mentioned in a previous post [Cordova plugins not working

[1]: Cordova plugins not working and the plugins are listed. Still javascript cannot use the plugin.

Am I missing something?

2
You config.xml suggests your are looking at old documentation. Unless you are working with an IDE, you should not be using <feature>. There is also no reason to look at you AndroidManifest.xml, unless you try a special configuration. As such, please read the next comment.user3255670
Please answer the following questions in your post. Since this appears to be your first post on this subject. Is this your first hybrid App? What is your target platform and their target versions: Android 4,5,6? iOS 7,8,9? What platform are you developing on? Windows, MacOS? Are you using CLI, SDK or Build ? Please do not assume the answer, please read the link. Once you have answer the question in the post, respond in the comments so I know you have added information to the post.user3255670
Is this your first hybrid App? yes What is your target platform and their target versions? Android verrsion 5.1 What platform are you developing on? windows Are you using CLI, SDK or Build ? cordova CLIPooja Srivastava
One problem you have to work on is following directions. Apparently, you can't follow directions. I will look at your Post now. I'll have a response within an hour.user3255670

2 Answers

1
votes

@Pooja,
one of the issues with Stackoverflow, the answer might be correct, but it may not apply to you. This is the case right now.

On #1) if you *are* using Cordova CLI, then it is already building the config.xml for you. So, there is no reason for you to create or modify your config.xml directly, except maybe to add your APP id or make some very special modifications.

On #2) The reason you app is not working is because you are using an example intended for someone using an IDE, like Eclipse or Android Studio. The SO question you found did not mention that and the answer made no mention of it either. I can tell this is the case because <feature> is NOT used for Corodova CLI.

I Quote Cordova Documentation -> The config.xml file -> The Feature Element

The feature Element

If you use the CLI to build applications, you use the plugin command to enable device APIs. This does not modify the top-level config.xml file, so the element does not apply to your workflow.

If you work directly in an SDK and using the platform-specific config.xml file as source, you use the tag to enable device-level APIs and external plugins. They often appear with custom values in platform-specific config.xml files. For example, here is how to specify the Device API for Android projects:

On #3, as stated in the comments, there is no reason for you to even be looking at the AndroidManifest.xml, unless you have a good reason. So this is nothing more than a red herring.

On #4, it is good that you have included cordova.js. It is a common mistake to leave this out. So, you are already ahead of the game.

On #5, on your error, it is likely that because your config.xml is not correct, that the "plugin" did not get loaded.

Lastly, because you are soooo far off base - making soooo many errors, I'm just going to give you the answer. ALSO, the mistakes you have made are not uncommon. I do this alot and this type of errors are very common.

YOUR FIXES

In short, you need to start over, build the "hello world" app, then add the plugin -- correctly. I can give you a quick fix, but it would only confuse you.

If you feel you learn better from videos, here are some videos I've reviewed, the will get moving in the right direction.

Lastly, since you have made some very common mistakes, this FAQ should also help.
Top Mistakes by Developers new to Cordova/Phonegap

0
votes

Remove the plugin you installed

cordova plugin rm org.apache.cordova.media

And install the latest version of the plugin with

cordova plugin add cordova-plugin-media