Phonegap Sound stops playing after calling playAudio() 30 to 40 times - android

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

Related

Using ZXing in Xamarin for Android, how do I stop continuous scanning right after I get my result?

I'm using ZXing in an Android app being developed in Xamarin to scan a QR code and start playing the corresponding audio file automatically.
My problem is that when I get a result from scanning, it takes some time for the audio player activity to load so it gets called twice or more due to subsequent successful scannings.
Is there a way to stop continuous scanning as soon as I get a correct result?
Here's the code:
//Start scanning
scanner.ScanContinuously(opt, HandleScanResult);
}
private void HandleScanResult(ZXing.Result result)
{
string msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
{
msg = result.Text;
var playerActivity = new Intent(myContext, typeof(AudioActivity));
//-------------------------------------------------------------
// Prerequisite: load all tracks onto "Assets/tracks" folder
// You can put here qr code - track assignments here below
// msg: decoded qr code
// playerActivity.Putextra second parameter is a relative path
// under "Assets" directory
//--------------------------------------------------------------
//Iterate through tracks stored in assets and load their titles into an array
System.String[] trackArray = Application.Context.Assets.List("tracks");
bool trackFound = false;
foreach (string track in trackArray)
{
if (track.Equals(msg + ".mp3"))
{
playerActivity.PutExtra("Track", "tracks/" + msg + ".mp3");
for (int i = 0; i < PostList.postList.Count; i++)
{
if (PostList.postList.ElementAt(i).code.Equals(msg))
playerActivity.PutExtra("TrackTitle", PostList.postList.ElementAt(i).title);
}
myContext.StartActivity(playerActivity);
trackFound = true;
}
}
Thank you!
Old question but i'll post it anyway for anyone still looking for this information.
You need your scanner to be a class variable. This is my code:
public MobileBarcodeScanner scanner = new MobileBarcodeScanner();
private void ArrivalsClick(object sender, EventArgs e)
{
try
{
if (Arrivals.IsEnabled)
{
MobileBarcodeScanningOptions optionsCustom = new MobileBarcodeScanningOptions();
scanner.TopText = "Scan Barcode";
optionsCustom.DelayBetweenContinuousScans = 3000;
scanner.ScanContinuously(optionsCustom, ArrivalResult);
}
}
catch (Exception)
{
throw;
}
}
private async void ArrivalResult(ZXing.Result result)
{
if (result != null && result.Text != "")
{
// Making a call to a REST API
if (resp.StatusCode == System.Net.HttpStatusCode.OK)
{
int? res = JsonConvert.DeserializeObject<int>(resp.Content);
if (res == 0)
{
scanner.Cancel(); // <----- Stops scanner (Something went wrong)
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("..", "..", "ΟΚ");
});
}
else
{
Plugin.SimpleAudioPlayer.ISimpleAudioPlayer player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
player.Load("beep.wav");
player.Play(); // Scan successful
}
}
else
{
scanner.Cancel();
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("..", "..", "ΟΚ");
});
}
}
}

voice sms in cordova android

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.

redirect user to a particular page after recieving notification in ngcordova push ionicframework android

is there a way with ionic to bring the user to a specific page or tab when opening the app through the notification?
I send the push,my phone is in background and the user accesses the app tapping on the notification just arrived. Actually the app opens the page which is already opened, but I want it opens a destination page chosen by notification.
// ------------------------------ GCM NG-CORDOVA PUSH-----------------------------------
.run(function($ionicPlatform,ngFB,$state,$cordovaPush,$rootScope,$ionicScrollDelegate, $cordovaMedia) {
$ionicPlatform.ready(function() {
var androidConfig = {
"senderID": "XXXXXXXXXXX"
};
$cordovaPush.register(androidConfig).then(function(result) {
// Success
// alert(result);
}, function(err) {
// Error
alert(err);
});
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
//alert(notification.event);
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
// alert('registration ID = ' + notification.regid);
}
break;
case 'message':
$ionicScrollDelegate.$getByHandle('chatScroll').scrollBottom();
// this is the actual push notification. its format depends on the data model from the push server
//alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
alert(JSON.stringify(notification));
if (notification.foreground)
{
// if the notification contains a soundname, play it.
var soundfile = notification.soundname;
// if the notification contains a soundname, play it.
var media = $cordovaMedia.newMedia("/assets/www/"+ soundfile);
media.play();
alert(notification.payload.message);
}
else
{
window.location = '#/home';
//$state.go('home');
}
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
});
// WARNING: dangerous to unregister (results in loss of tokenID)
// $cordovaPush.unregister(options).then(function(result) {
// Success!
//}, function(err) {
// Error
//})
});
})
Please Help me. Where i am doing wrong?

infobip phonegap plugin crash

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?

How to download and save a file using phonegap/cordova on Android

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

Categories

Resources