Phonegap Media.play - android

I am trying to set up a PhoneGap app on Android using the Media object. But no audio is being played on my Android device for some reason.
The source code of the HTML file is:
<input type="button" onclick="playAudio()" value="Play Sound">
<script>
function playAudio() {
var my_media = new Media("http://www.noiseaddicts.com/samples/3721.mp3");
my_media.play();
}
</script>
I have added the plugin and site whitelisting to the config.xml file like this:
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = XXXX
version = "0.1.0" >
<gap:platform name="android" />
<plugin name="Media" value="org.apache.cordova.AudioHandler" />
<access origin="http://www.noiseaddicts.com" />
</widget>
Any idea of what I could have done wrong?

You should attach success and error handlers to be able to debug what is going on.
var my_media = new Media("http://www.noiseaddicts.com/samples/3721.mp3",
// success callback
function () {
alert("playAudio():Audio Success");
},
// error callback
function (err) {
alert("playAudio():Audio Error: " + err);
}
);
my_media.play();
Update: If this is not working try doing the following. In app/res/xml/config.xml:
<feature name="Media">
<param name="android-package" value="org.apache.cordova.media.AudioHandler" />
</feature>
In app/AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
which is specified here: http://cordova.apache.org/docs/en/3.1.0/cordova_media_media.md.html#Media_accessing_the_feature

The problem was that I did not create the right file structure when I created the project and was missing critical files including the cordova.js file. I started over from the beginning according to cordova documentation and got it working then.

Related

Issues with android permissions in Cordova

I'm working on a demo app for android that needs to get information about the phone, the SIM, the network, etc.. I put all the code in a plugin and my Java function that that retrieves the SIM info looks like this:
private boolean getSIMInfo(CallbackContext cbc) throws JSONException {
// tm is a TelephonyManager instantiated in initialize
JSONObject res = new JSONObject()
.put("carrierID", tm.getSimCarrierId())
.put("carrierName", tm.getSimCarrierIdName())
.put("countryIso", tm.getSimCountryIso())
.put("operator", tm.getSimOperator())
.put("operatorName", tm.getSimOperatorName())
.put("state", tm.getSimState())
.put("msisdn", tm.getLine1Number()) // <== Requires permission
;
cbc.success(res);
return true;
}
As long as I dont call getLine1Number() everything goes fine.
getLine1Number() requires either android.permission.READ_PHONE_STATE or a.p.READ_SMS or a.p.READ_PHONE_NUMBERS to be set. I first declared a.p.READ_PHONE_STATE in plugin's plugin.xml and I checked it was injected into AndroidManifest.xml.
The platform part of the plugin.xml looks like this:
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="TelPlugin">
<param name="android-package" value="org.buguigny.CordovaPlugin.TelPlugin"/>
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml" />
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</config-file>
<source-file src="src/TelPlugin.java" target-dir="src/org/buguigny/CordovaPlugin/TelPlugin" />
</platform>`
Upon execution I get the error:
getLine1NumberForDisplay: Neither user 10190 nor current process has
android.permission.READ_PHONE_STATE, android.permission.READ_SMS, or
android.permission.READ_PHONE_NUMBERS
I tried with the other permissions: same error.
Any idea what I'm doing wrong ?
On Android >=6.0, Android app should request permission runtime.
So in Cordova, you can use the permission plugin like following
permissions.requestPermission(permissions.READ_PHONE_STATE, success, error);
function error() {
console.warn('Camera permission is not turned on');
}
function success( status ) {
if( !status.hasPermission ) error();
}

How can I play a sound in onclick event in PhoneGap/Cordova?

I've been searching for hours but really I don't find answer, and many of the answers that I find, don't work for me.
I'm working on an entertainment application where there are several buttons and pressing each one, generates a different sound. The problem is that I can't get play the sound. I tried the plugin half, and another things, and I really think I'm too lost haha, I hope you can help me. I am a beginner at this, so any comments / suggestions / help is more than welcome, really.
This is my config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.carlosmdnv.appname" version="1.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"
xmlns:gap="http://phonegap.com/ns/1.0">
<name>App Title</name>
<description>
Description here.
</description>
<author email="contacto#carlosmdnv.com" href="http://www.carlosmdnv.com">
Carlos Medina
</author>
<content src="index.html" />
<access origin="*" />
<icon src="www/img/icon.png"/>
<gap:plugin name="org.apache.cordova.media" />
<gap:plugin name="org.apache.cordova.core.Device"/>
<feature name="Media">
<param name="android-package" value="org.apache.cordova.AudioHandler" />
</feature>
</widget>
My index.js:
document.addEventListener("deviceready", function(){
function playAudio(id) {
var audioElement = document.getElementById(id);
var url = audioElement.getAttribute('src');
var my_media = new Media('/android_asset/www/' + url,
// success callback
function () { console.log("playAudio():Audio Success"); },
// error callback
function (err) { console.log("playAudio():Audio Error: " + err); }
);
// Play audio
my_media.play();
});
And my index.html elements:
<div onclick="playAudio('successSound')"></div>
<audio id="successSound" src="mp3/sound.mp3" type="audio/mpeg"></audio>
Thanks in advance for the help!

The Cordova Contacts API is inaccessible

I am trying to access Cordova Contacts API to access the device contact details but i am not able to get the details .I have added the reference of Cordova.js and have added below in Conf.xml ..
<feature name="Contacts">
<param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
</feature>
and i have given permission in AndroidManifest.xml file too..
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
Still on running my application in the OnLoad() event function only i am getting this error because of undefined ..Here is the code..
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
$("#searchby_chooser_ok_button").bind ("click", searchByCriteria);
if (typeof Contact === "undefined") {
getElement("contacts_list").innerHTML = "<p>The Cordova Contacts API is inaccessible</p>";
}
}
I tried my level best to search on web in vain only .Is there any specific file that i need to add into the project?..Please help me to resolve this ..Thanks..Here is my project directory structure
Read the following links for phonegap/cordova project.
http://thejackalofjavascript.com/phonegap-3-cli-setup-mac-windows/
http://coenraets.org/blog/cordova-phonegap-3-tutorial/
http://teusink.blogspot.in/2013/07/guide-phonegap-3-android-windows.html
http://sdk.revmobmobileadnetwork.com/phonegap_cordova.html
http://docs.phonegap.com/en/3.5.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide
Phonegap Cordova installation Windows

How to add permissions to Android PhoneGap project?

A plugin I am using to access the AccountManager in android through phonegap requires some permissions. I am using PhoneGap 3.4.0. In the config.xml in my www folder there is a tag
<preference name="permissions" value="none"/>
I am having the hardest time figuring out how I add permissions to this config.xml file. All the other questions I've found say to alter the manifest file, but that feels wrong with me setting so many things in this config.xml file.
How do I add the permissions in the config.xml file? Comma separated list in the tag referenced above?
Here are the permissions I have added to my manifest file:
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
Please check you have declared the following.
<feature name="NetworkStatus">
<param name="android-package" value="CDVConnection" />
</feature>
Network status will be the feature name and value will be your class name.
In Android Manifest,
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Aso check cordova_plugin.js
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [{
"file": "plugins/org.apache.cordova.dialogs/www/notification.js",
"id": "org.apache.cordova.dialogs.notification",
"merges": ["navigator.notification"]
}, {
"file": "plugins/org.apache.cordova.network-information/www/network.js",
"id": "org.apache.cordova.network-information.network",
"clobbers": ["navigator.connection", "navigator.network.connection"]
}];
module.exports.metadata = // TOP OF METADATA
{
"org.apache.cordova.device": "0.2.8",
"org.apache.cordova.network-information": "0.2.7"
}
});
These are some permission types.
Thanks.

Phonegap Android InAppBrowser not working

I am working on an Android App with Phonegap Cordova-3.0.0 and when I call InAppBrowser I got MotionEvent mTouchMode = 4 error. And InAppBrowser function is not working. So how can I fix this? Do I need do some setting on AndroidManifest.xml or config.xml?
And I got this on my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And I got this on my config.xml
<plugins>
<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />
</plugins>
In Phonegap Cordova-3.0.0 version, for the app to communicate closely with various device-level features, we need to add plugins that provide access to core Cordova APIs.
The cordova plugin add command requires you to specify the repository for the plugin code.
For example, In-app browser:
$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git
we need run this in command line. Don't need worries about AndroidManifest.xml or config.xml files. After you run $ cordova build, it will auto write for you.
You could get more idea about it in doc.phonegap
Add following code in config.xml this works fine for mi.
<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />
<access origin="*" browserOnly="true"/>
You have to mention the below code line in config.xml
<plugin name="InAppBrowser" value="CDVInAppBrowser" />
try to add this to your manifest, it helps me with a f*king plugin to work
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
try also not use 'plugin' tag in config.xml, but:
<feature name="InAppBrowser">
<param name="android-package" value="org.apache.cordova.InAppBrowser"/>
</feature>
it will help for future updates of phonegap
To open a link in browser inside the app without opening external browser
HTML
<input type="button" id="button1" value = "click here"
onclick="window.open('https://example.com','_blank','location=yes','closebuttoncaption = Return');">
Now go into your project folder and open Terminal or Command Prompt (Windows) and type the following command:
cordova plugin add cordova-plugin-inappbrowser --save
It will configure the required files and also add the plugin into your config.xml file.
Open your HTML page where you're trying to open the link, and put this JavaScript.
<script src="cordova.js"></script>
<script type = "text/javascript" charset = "utf-8">
function onLoad(){
document.addEventlistner("deviceready", OnDeviceReady, false);
}
function onDeviceReady(){
}
</script>

Categories

Resources