We are working on hydrid apps. Recently we got the requirement to support voice sms. We did R&D on that but we didn't have any luck. So we are trying to record 1 minute voice then we send it to a server. We don't know is it correct way. Please guide to us.
We tried like this https://www.npmjs.com/package/cordova-plugin-audio-recorder-api
var recorder = new Object;
function stop(){
recorder.stop = function() {
window.plugins.audioRecorderAPI.stop(function(msg) {
// success
alert('ok: ' + msg);
}, function(msg) {
// failed
alert('ko: ' + msg);
});
}}
function record{
recorder.record = function() {
window.plugins.audioRecorderAPI.record(function(msg) {
// complete
alert('ok: ' + msg);
}, function(msg) {
// failed
alert('ko: ' + msg);
}, 30); // record 30 seconds
}}
function playback(){
recorder.playback = function() {
window.plugins.audioRecorderAPI.playback(function(msg) {
// complete
alert('ok: ' + msg);
}, function(msg) {
// failed
alert('ko: ' + msg);
});
}}
We please guide to proper way to achieve our task.
Related
I'm using infobip to deliver push notifications to my apps and i have to admit that it is really powerful.
Anyways i've got a question. Does the cordova plugin work properly? Because my app doesn't recognize the push object (obviously the plugin is installed).
Another question, Must I initialize the plugin in a .js file in all the html page of my app or is enough to call the initialize and register method only in the first .js that the app loads? (index.js for me)
These are some code rows
var senderId1 = "", applicationId1, applicationSecret1;
function notificationListener(event, notification){
switch(event){
case ("onNotificationReceived"):
alert(notification.title);
break;
case ("onInvisibleNotificationReceived"):
// TODO your code here
break;
case ("onNotificationOpened"):
window.location.href = "promo.html";
break;
case ("onUnregistered"):
// TODO your code here
break;
case ("onRegistered"):
// TODO your code here
break;
case ("onError"):
// TODO your code here
break;
default:
// TODO your code here
break;
}
};
//inside the 'onDevideReady' function
if (getMobileOperatingSystem() == 'Android') {
senderId1 = "012345";
applicationId1 = "djdjdjdjdjdj";
applicationSecret1 = "0123456789abcd";
} else {
applicationId1 = "djdjdjdjdjdj";
applicationSecret1 = "0123456789abcd";
push.setBackgroundLocationUpdateModeEnabled(true);
}
//pushnotification
push.initialize(notificationListener);
var text = '{' +
'"regData": {' +
' "applicationId": "' + applicationId1 + '",' +
' "applicationSecret": "' + applicationSecret1 + '",' +
' "senderId": "' + senderId1 + '"' +
'}' +
'}';
var regData = JSON.parse(text);
push.register(regData);
push.enableLocation();
The error i had in xCode is (unfortunately i can't upload an image :( )
#import <UIKit/UIKit.h>
int main(int argc, char* argv[])
{
#autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate"); //ERROR HERE
return retVal;
}
}
I need to publish this app as soon as possible so thanks for the help.
EDIT:
I change a little my code
plug = window.localStorage.getItem("enable");
alert(plug);
if (plug == null) {
//pushnotification
push.initialize(notificationListener());
alert('here');
if (getMobileOperatingSystem() == 'Android') {
senderId1 = "902945349798";
applicationId1 = "ftscfg8eb5kr";
applicationSecret1 = "qowk5x0q93a30az";
} else {
applicationId1 = "ftscfg8eb5kr";
applicationSecret1 = "qowk5x0q93a30az";
//enable the background location
push.setBackgroundLocationUpdateModeEnabled(true);
}
alert('here2');
/*
var text = '{' +
'"regData": {' +
' "applicationId": "' + applicationId1 + '",' +
' "applicationSecret": "' + applicationSecret1 + '",' +
' "senderId": "' + senderId1 + '"' +
'}' +
'}';
var regData = JSON.parse(text);
*/
//register the push
push.register({
senderId: senderId1,
applicationId: applicationId1,
applicationSecret: applicationSecret1
});
alert('here3');
//enable the location
push.enableLocation();
window.localStorage.setItem('enable', '1');
alert('here4');
}
So, in this case i save a local variable and only if it's the first time i launch the app i set the push parameters. All the alerts works fine and the app works...but i can't receive the notification :(
function notificationListener (event, notification){
switch(event){
case ("onNotificationReceived"):
alert(notification.title);
break;
case ("onInvisibleNotificationReceived"):
// TODO your code here
break;
case ("onNotificationOpened"):
window.location.href = "promo.html";
break;
case ("onUnregistered"):
// TODO your code here
break;
case ("onRegistered"):
// TODO your code here
break;
case ("onError"):
// TODO your code here
alert(notification);
break;
default:
// TODO your code here
break;
}
};
This is my function. When i send a notification using infobip's panel the event OnNotificationreceived will activate...but it's not like this. Even onError doesn't work and the senderID, applicationID and applicationsecret are correct.
Maybe you have to wait for ios is ready.
I initialized the plugin this way:
document.addEventListener("deviceready", initInfoBitPlugin, false);
It works for me.
It should be enough to initialize plugin only once.
Do you get onError event in your notification listener? What do you get as a argument there?
In onNotificationReceived, what is the type of notification argument? Could you JSON.stringify it to logs?
Can you send some logs surrounding this issue?
In my app i have given sound response on mouse click. This is how i have done
i have called the function like this
<center><input id="click_flip" type="button" value="Click Me" class="clickme" onclick="callplay()"></center>
here is the function
<script type="text/javascript" charset="utf-8">
function callplay()
{
if(voice=="male")
playAudio('/android_asset/www/Mobile/sound/Male/'+rand1+'.mp3');
else
playAudio('/android_asset/www/Mobile/sound/Female/'+rand1+'.mp3');
}
// Audio player
//
var my_media = null;
// Play audio
//
function playAudio(src) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
// else play current audio
// Play audio
my_media.play();
}
// onSuccess Callback
//
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
// alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
But when i am repeating the button click multiple (around 30 to 40 ) times. The sound is not giving any responses.
After that using this link i have added this function
if(my_media){
my_media.stop();
my_media.release();
}
also tried this
function playAudio(url) {
try {
var my_media = new Media(url,
// success callback
function () {
**my_media.release();**
},
// error callback
function (err) {
**my_media.release();**
});
// Play audio
my_media.play();
} catch (e) {
alert(e.message);
}
}
but not working. Please suggest
I have the same problem, and made a workaround by releasing the previous media before creating a new one to play:
var my_media = null;
function playAudio(src)
{
if(my_media != null) my_media.release();
my_media = new Media(src);
my_media.play();
}
If you release the hardware you don't need to stop, because of the release automatically stops the current media. Somehow if you try to release in the success or in the error callback it does not always work.
There are times (especially with user inputs) when if you try to play a new media before the previous one is not finished, the whole thing just breaks.
In My case i have scanning app with phone gap.
and it vibrates and beep fail when scan fails via our API scanning ticket.
and there is success sound without vibration via our API scanning ticket.
After 35 to 40 times it fails to either vibrate or play sound.
Code Before Fix:
In HTML
<audio id="successSound"
src="/android_asset/www/audio/correct.mp3"
type="audio/mpeg">
</audio>
var my_media = null;
var mediaTimer = null;
function playAudio(id) {
var audioElement = document.getElementById(id);
var src = audioElement.getAttribute('src');
// Create Media object from src
// alert(src);
// alert(getPathMedia());
my_media = new Media(src, onSuccess, onError);
// Play audio
my_media.play();
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// get my_media position
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition((position) + " sec");
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
// Pause audio
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
// Stop audio
function stopAudio() {
if (my_media) {
my_media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// onSuccess Callback
//
function onSuccess() {
// alert('success');
}
// onError Callback
function onError(error) {
switch(error.code){
case MediaError.MEDIA_ERR_ABORTED:
alert('MEDIA_ERR_ABORTED code: ' + error.code);
break;
case MediaError.MEDIA_ERR_NETWORK:
alert('MEDIA_ERR_NETWORK code: ' + error.code);
break;
case MediaError.MEDIA_ERR_DECODE:
alert('MEDIA_ERR_DECODE code: ' + error.code);
break;
case MediaError.MEDIA_ERR_NONE_SUPPORTED:
alert('MEDIA_ERR_NONE_SUPPORTED code: ' + error.code);
break;
default:
{
alert('Un Known: ' + error.code);
navigator.notification.vibrate(2000);
playAudio("errorSound");
}
}
}
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
Code After Fix:
var failCounter = 0;
var successCounter = 0;
var srcSuccess = "/android_asset/www/audio/correct.mp3";
var srcFail = "/android_asset/www/audio/error_long.mp3";
var my_media_success = null;
var my_media_fail = null;
function playAudioSuccess() {
// stopAudio(my_media);
if (my_media_success != null) {
my_media_success.release();
}
successCounter = successCounter + 1;
// Create Media object from src
// alert(src);
// alert(getPathMedia());
try {
my_media_success = new Media(srcSuccess, onSuccess, onError);
// my_media.setVolume('1.0');
// if (successCounter >= 35) {
// alert("success count " + successCounter + " total counting " + ( successCounter + failCounter));
// }
// Play audio
my_media_success.play();
} catch (err) {
alert(err);
}
}
function playAudioFail() {
try {
// stopAudio(my_media);
if (my_media_fail != null) {
my_media_fail.release();
}
failCounter = failCounter + 1;
// Create Media object from src
// alert(src);
// alert(getPathMedia());
my_media_fail = new Media(srcFail, onSuccess, onError);
// my_media_fail.setVolume('1.0');
// if (failCounter >= 35) {
// alert("fail count " + failCounter + " total counting " + ( successCounter + failCounter));
// }
// Play audio
my_media_fail.play();
} catch (err) {
alert(err);
}
}
// Pause audio
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
// Stop audio
function stopAudio(my_media) {
if (my_media) {
my_media.stop();
}
// clearInterval(mediaTimer);
// mediaTimer = null;
}
// onSuccess Callback
//
function onSuccess() {
// alert('success');
}
// onError Callback
function onError(error) {
switch(error.code){
case MediaError.MEDIA_ERR_ABORTED:
alert('MEDIA_ERR_ABORTED code: ' + error.code);
break;
case MediaError.MEDIA_ERR_NETWORK:
alert('MEDIA_ERR_NETWORK code: ' + error.code);
break;
case MediaError.MEDIA_ERR_DECODE:
alert('MEDIA_ERR_DECODE code: ' + error.code);
break;
case MediaError.MEDIA_ERR_NONE_SUPPORTED:
alert('MEDIA_ERR_NONE_SUPPORTED code: ' + error.code);
break;
default:
{
alert('Un Known: ' + error.code);
navigator.notification.vibrate(1000);
setTimeout(function() {
playAudioFail();
}, delayInMilliseconds);
}
}
}
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
After lot of try and error and alerts here and there !
1) I avoided getElementById to get the src path i just defined it as global variable.
2) I separated the methods of success and fail ( refactoring )
3) I made the variables local as much as possible to avoid any memory leaks and overlapping and exceed stack etc.. stuff.
4) I removed the setAudioPosition method because its not used and called without a reason.
( lesson learned) when you take your code from internet be carefull of what you really need.
5) I noticed when i added alerts it worked but when i removed them it did not work , so my
MAIN SOLUTION was adding Timeoutbetween vibrate which is called first , and the play method at critical section like this
navigator.notification.vibrate(1000);
setTimeout(function() {
playAudioFail();
}, delayInMilliseconds);
6) I added release method , but
(leasson learned) be aware where to put it
so I put it before calling the play it self on correct reference to same variable ofcourse.
if (my_media_fail != null) {
my_media_fail.release();
}
7) Last advice as its javascript you can use try and catch just in case ))
Try catch W3School
i have a problem with titanium to comunicate with a my server in the network,
the server's ip is 192.168.0.208 on the port 8000 (is a Node.js server).
If i call the server from the browser no problem but if i try to call the server from the application in Titanium i saw this error "The target server failed to respond" and in the server log no call is received
this is my network.js file in the application
function HttpRequest(url, type, args, functionOnSuccss,functionOnError,timeout) {
// args is json parameters OPTIONAL
Ti.API.log("[HTTP REQ] Call" + url);
// ---# ok string ------
var xhr = Titanium.Network.createHTTPClient();
xhr.open(type,url);
xhr.cache = false;
xhr.enableKeepAlive = false;
xhr.timeout = timeout ? timeout : 500000;
xhr.setRequestHeader("Content-type", "application/json");
// xhr.setRequestHeader("Cookie", 'JSESSIONID=' + cookie + '');
xhr.onload = function(e) {
Ti.API.info("[HTTP] Response" + this.responseText);
functionOnSuccss(this.responseText);
};
xhr.onerror = function(e) {
Ti.API.info("ERROR " + e.error);
// alert("Connection Error");
functionOnError(e.error);
};
if(args){
xhr.send(args);
}else{
xhr.send();
}
};
exports.request = HttpRequest;
and this is the coda that make the request
network = require('/library/network');
var sendCarrello = function() {
$.loader.visible = true;
$.carrelloCamminante.animate(a);
url = "192.168.0.208:8000/newMobileUser"; // or http://192.168.0.208:8000/newMobileUser it's the same
network.request(url, "get",undefined, function(resp) {
alert(resp);
$.loader.visible = false;
}, function(err) {
alert("error - "+""+err);
});
};
what could be the error?
You must use "GET" not "get" :
network.request(url, "GET",undefined, function(resp) { ....
I get the error INVALID_REQUEST when i try to show a IMInterstitialAdView.
I run the app on real device, no in test mode.
I deployed the app on my device via titanium store, no google play for now.
Here the code:
var Inmobi = require('ti.inmobi.ad');
Inmobi.setLogLevel(1);
Inmobi.sendAppTrackerConversion("dd1efdd608c7424cbd675f55d8f5a0f1");
var request = Inmobi.createIMAdRequest({
//locationInquiryAllowed : true,
// currentLocation: undefined,
testMode : false
});
var inmobiInter = Inmobi.createIMInterstitialAdView({
// activity: currentActivity,
imAppId : "dd1efdd608c7424cbd675f55d8f5a0f1"
});
var currActivity = Titanium.Android.currentActivity;
inmobiInter.load(currActivity);
// listener for onAdRequestLoaded
inmobiInter.addEventListener("onAdRequestLoaded", function() {
alert("Ad request loaded");
Ti.API.info("Ad request loaded");
});
// listener for onAdRequestFailed
inmobiInter.addEventListener("onAdRequestFailed", function(e) {
alert("Ad request failed, error Code: " + e.errorCode);
Ti.API.info("Ad request failed, error Code: " + e.errorCode);
});
// listener for onShowAdScreen
inmobiInter.addEventListener("onShowAdScreen", function() {
alert("Show Ad Screen");
Ti.API.info("Show Ad Screen");
});
// listener for onDismissAdScreen
inmobiInter.addEventListener("onDismissAdScreen", function() {
alert("Dismiss Ad screen");
Ti.API.info("Dismiss Ad screen");
});
// listener for onLeaveAdScreen
inmobiInter.addEventListener("onLeaveApplication", function() {
alert("Leave Application");
Ti.API.info("Leave Application");
});
inmobiInter.loadNewAd(request);
self.add(inmobiInter);
Ti.API.info('======================================================');
// listener
buttonMPView.addEventListener('click', function() {
Ti.API.info('------------> ' + inmobiInter.getState());
if (inmobiInter.getState() == inmobiInter.State_READY) {
inmobiInter.show();
} else {
Ti.API.info("State is not " + inmobiInter.State_READY + ", State is: "
+ inmobiInter.getState());
}
if(Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){
alert("Nessuna connessione internet presente.")
}else{
self.fireEvent('clickButtonMPView')
}
});
Any hint?
Thank you
I am Naresh and I work for InMobi.
You are using the older plugin and we don't recommend that.
Please use the following updated InMobi plugin for Titanium. Documentation is on the same page. It also includes InMobi analytics.
http://www.inmobi.com/support/art/23846096/22114287/appcelerator-partner-platform-integration-guide/
Do let me know if you have questions.
thanks
I am at my whit's end.
I will try to keep it brief.
Using Cordova/Phonegap 3.0 (and get the same results on 2.8.0).
Android version 4.0.4.
Code works on BlackBerry10 (Q10 and Z10).
On Android it errors with a JSON Error (no, I'm not parsing JSON, this seems to come out of cordova's bowels). I will paste the JSON.stringified error object at the end of this.
So, on to code then:
First a filesystem success function:
function onFSSuccess(fileSystem) {
if (fileSystem == null) {
window.alert("fileSystem is null");
}
var root = fileSystem.root;
root.getDirectory("com.app.id",{create:true},gotDir,onError);};
Then a function to handle success with directory retrieval:
function gotDir(d){
DATADIR = d;
doTheDl (d.fullPath + "/update.sql",fileTransfer);
};
Then the actual call to get the filesystem:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, null);
Then a function to download the file:
function doTheDl (localPath,fileTransfer) {
try {
window.alert ("Downloading to '" + localPath + "'");
fileTransfer.download (
uri,
localPath,
function (entry) {
try {
$("#dbDownloadProgressContainer").text("File saved to " + entry.name + ". Applying script to database...");
dbInitObj.applyUpdateScript(entry);
}
catch (e) {
window.alert ( e);
}
},
function (err) {
window.alert ("ERROROR!!! - " + err);
var errCodeName = err.code;
switch (err.code) {
case FileTransferError.FILE_NOT_FOUND_ERR:
errCodeName ='FILE_NOT_FOUND_ERR';
break;
case FileTransferError.INVALID_URL_ERR:
errCodeName="INVALID_URL_ERR";
break;
case FileTransferError.CONNECTION_ERR:
errCodeName="CONNECTION_ERR";
break;
case FileTransferError.ABORT_ERR:
errCodeName="ABORT_ERR";
break;
default:
errCodeName = "UNKNOWN";
break;
}
window.alert ("Download failed: " + err.source + ", " + err.target + ", " + errCodeName);
},
true
);
}
catch (e) {
window.alert ( e);
}
}
Man, gotta love all these async callbacks...
Next we get to the heart of the matter, trying to READ the downloaded file:
//Bulk of applyUpdateScript script ommited, but eventually it gets here:
function readComplete (evt) {
$("#dbDownloadProgressContainer").text("Parsing script file...");
//Got this gem from here: http://beckism.com/2010/09/splitting-lines-javascript/
var lines = evt.target.result.match(/^.*([\n\r]+|$)/gm);
//var lineIndx = lines.length;
window.setTimeout(function () {
$("#dbDownloadProgressContainer").text("Processing " + lines.length + " statements");
},50);
};
try {
var fileReader = new FileReader();
fileReader.onloadend=readComplete;
fileReader.onerror=function (err) {
//var errStr = translateFileError (err);
window.alert ("FileReader.onerror: " +JSON.stringify (err));
};
fileReader.onloadstart=function (evt) {
window.alert ("FileReader.onloadstart - " + JSON.stringify (evt));
};
fileReader.onload=function (evt)
{
window.alert ("FileReader.onload - Called when the read has successfully completed.- " + JSON.stringify (evt));
};
fileReader.onprogress = function (evt)
{
window.alert ("FileReader.onprogress - " + JSON.stringify (evt));
}
fileReader.onabort = function (evt)
{
window.alert ("FileReader.onabort - " + JSON.stringify (evt));
}
function gotFile (fileEntry) {
window.alert ("Activating reader for file '" + fileEntry.fullPath + "'");
fileReader.readAsText(fileEntry);
};
function noFileFound (fileError) {
alert ("Can not access database update script: code " + translateFileError (fileError));
};
// window.alert ("scriptPath.name = " + scriptPath.name);
DATADIR.getFile (scriptPath.name,null,gotFile,noFileFound);
}
catch (e) {
window.alert (e);
}
NOW, when I hit the reading bits, I eventually get this from the 'onerror' event (rember this is the JSON.stringfied error object:
{
"type":"error",
"bubbles":false,
"cancelBubble":false,
"cancelable":false,
"lengthComputable":false,
"loaded":0,
"total":0,
"target":{
"_readyState":2,
"_error":{
"code":"JSON error"
},
"_result":null,
"_fileName":"file:///mnt/sdcard/com.app.id/update.sql",
"_realReader":{
"error":null,
"result":"",
"readyState":0
}
}
}
Please also note that 'com.app.id' is a place holder for the actual app ID - can't paste that for fear of sensitive names. I did try other folder names as well.
Other notable(?) items:
The download progress event seems to indicate that we are downloading precisely double the actual file size (wtf?)
Results are the same on android device and emulator
BlackBerry10 seems to work fine
Thanks in advance to any clever people....
OK.
This was the solution:
function gotFile (fileEntry) {
fileEntry.file (function (file) {
fileReader.readAsText(file);
});
};
So thanks a BAJILLION to this dude:
http://www.html5rocks.com/en/tutorials/file/filesystem/?ModPagespeed=noscript
In case you missed it, the magic is the call to the "file(...)" function on the fileEntry object.
Why it works on the BB10 WITHOUT it....aarrgggghhh