cordova-plugin-media-with-compression error on IOS when recording - ios

I am using cordova-plugin-media-with-compression for an app which runs on IOS and Android. On Android the code is working perfectly, records, play, stop, erase, no problem. On IOS I can play the files saved using the android app, but as soon as I try to record I get error 1 which is MEDIA_ERR_ABORTED (no idea what that means). So works perfectly on Android, but will not record on IOS. I am also using cordovafile for other things and they work, so I know cordova file is working and I verified that I am getting a legitimate folder for the mediapath.
I used the code right out of the example.
$scope.mediapath = cordova.file.externalApplicationStorageDirectory || cordova.file.tempDirectory || cordova.file.sharedDirectory;
$rootScope.mediaOptions = {
SampleRate: 16000,
NumberOfChannels: 1
};
$scope.mediafile = "record-audio.m4a";
$scope.media = new Media($scope.mediapath + $scope.mediafile, function () {
console.log("recordAudio():Audio Success");
},
function (err) { console.log("recordAudio():Audio Error: " + err.code); },
function (s) {
if (s === 4) {
$timeout(function () {
angular.element(document.getElementById('stopButton')).triggerHandler('click');
}, 0);
}
} );
$scope.media.startRecordWithCompression($rootScope.mediaOptions);

Even though . $scope.mediapath = $scope.mediapath.fullPath; worked for a while, I had further errors and ended up with this solution. Note I keep both mediapath and mediapath1 because when using $cordovaFile.readAsDataURL($rootScope.mediapath, $scope.mediafile)... I need the mediapath for ios and android.
$rootScope.mediaOptions = {
SampleRate: 16000,
NumberOfChannels: 1
};
$rootScope.mediapath = cordova.file.tempDirectory || cordova.file.dataDirectory || cordova.file.externalApplicationStorageDirectory || cordova.file.sharedDirectory || cordova.file.applicationStorageDirectory;
$rootScope.mediapath1 = $rootScope.mediapath;
if (cordova.platformId === 'ios') {
$rootScope.mediapath1 = '';
}
then when I need to initialize the media
$scope.mediafile = "audio.m4a";
$scope.media = new Media($rootScope.mediapath1 + $scope.mediafile,
function () {
console.log("recordAudio():Audio Success");
},
function (err) { console.log("recordAudio():Audio Error: " + err.code + err.message); },
function (s) {
// catch change to audio here for example when s===4 the recording has been stopped.
}
});
$scope.media.startRecordWithCompression($rootScope.mediaOptions)
;

Related

Audio file does not persist in Cordova with LocalFileSystem.PERSISTENT

I have been trying to store Audio file in persistent storage for two days without success.
So far I am able to create an audio file which records audio from Microphone (The app has the permission) using the code attached below.
The audio file is getting generated & stored successfully, I can play it.
But the real problem is when I close the app and come back and try to play the file it shows error.
"{"message": "Cannot use audio file from resource '/myrecording.wav'",
"code":1}"
The file is not persistent across app sessions even though I used LocalFileSystem.PERSISTENT.
I am not sure whether the problem is with my Media/Audio code or File storage code.
Please find the code attached below:
Below function records the audio from the microphone.
function _recordAudio() {
var deferred = $q.defer();
var src = "myrecording.wav";
alert("SRC:" + src);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(src, {
create: true,
exclusive: false
}, function (fileEntry) {
alert("File " + src + " created at " + fileEntry.fullPath);
var mediaRec = new Media(fileEntry.fullPath,
function () {
alert("Success");
}, function (error) {
alert("error:" + JSON.stringify(error));
});
// Record audio
mediaRec.startRecord();
// Stop recording after 10 sec
var recTime = 0;
var recInterval = setInterval(function () {
recTime = recTime + 1;
if (recTime >= 5) {
clearInterval(recInterval);
mediaRec.stopRecord();
deferred.resolve(fileEntry.fullPath);
}
}, 1000);
}, function (error) {
alert("getFile error:" + JSON.stringify(error));
deferred.reject();
}); //of getFile
}, function (error) {
alert("requestFileSystem error:" + JSON.stringify(error));
deferred.reject();
}); //of requestFileSystem
return deferred.promise;
}
Below function plays the audio.
function _play2() {
var src = "myrecording.wav";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(src, null, function (fileEntry) {
alert("File " + src + " created at " + fileEntry.fullPath);
var mediaRec = new Media(fileEntry.fullPath,
function () {
alert("Success play2");
}, function (error) {
//Getting error after closing and opening the app
//Error message = {"message": "Cannot use audio file from resource '/myrecording.wav'","code":1}
alert("error play2:" + JSON.stringify(error));
});
mediaRec.play();
});
});
}
I solved this problem by passing cdvfile: path to the Media plugin in PlayAudio function code and copying the file from Temp storage to persistent storage.
I had to use localURL of the file.
This part solved my problem:
fileEntry.file(function (file) {
_playNow(file.localURL);
}
For full functions refer code snippets below:
recordAudio: function (projectNo, ItemNo) {
try {
var deferred = $q.defer();
var recordingTime = 0;
_audioLoader = $("#audioLoader");
_audioLoader.show();
UtilityService.showPopup('audio');
_isRecording = true;
_recordFileName = "Audio_" + projectNo + "_" + ItemNo + ".wav";
_mediaRecord = new Media(_recordFileName);
//Record audio
_mediaRecord.startRecord();
var recordingInterval = setInterval(function () {
recordingTime = recordingTime + 1;
$('#audioPosition').text(_secondsToHms(recordingTime));
if (!_isRecording) {
clearInterval(recordingInterval);
_mediaRecord.stopRecord();
_mediaRecord.release();
deferred.resolve();
}
}, 1000);
//document.getElementById('audioPosition').innerHTML = '0 sec';
$('#audioPosition').text('0 sec');
return deferred.promise;
}
catch (ex) {
alert('WMMCPA|recordAudio:- ' + ex.message);
}
},
Get file path from the persistent storage and send it to the play method.
//To play recorded audio for specific project item
playAudio: function (projectNo, ItemNo) {
try {
_recordFileName = "Audio_" + projectNo + "_" + ItemNo + ".wav";
var newFileUri = cordova.file.dataDirectory + _recordFileName;
window.resolveLocalFileSystemURL(newFileUri, function (fileEntry) {
fileEntry.file(function (file) {
_playNow(file.localURL);
}, function (error) {
alert("WMMCPA|playAudio.file:-" + JSON.stringify(error));
});
}, function (error) {
alert("WMMCPA|playAudio.resolveLocalFileSystemURL:-" + JSON.stringify(error));
});
}
catch (ex) {
alert("WMMCPA|playAudio:-" + ex.message);
}
}
function _playNow(src) {
try {
var mediaTimer = null;
_audioLoader = $("#audioLoader");
_audioLoader.show();
UtilityService.showPopup('audio');
//Create Media object from src
_mediaRecord = new Media(src);
//Play audio
_mediaRecord.play();
} catch (ex) {
alert('WMMCPA|_playNow.mediaTimer:- ' + ex.message);
}
}, 1000);
} catch (ex) {
alert('WMMCPA|_playNow:- ' + ex.message);

Cordova iOS app crashes after second launch on device

after a lot of investigation i have no clue of what could be the problem here. I have an iOS app developed with cordova and using Firebase (cordova-plugin-firebase) for notifications.
When i run my app on my device with Xcode for the first time everything works fine, the notifications arrive and the app works great. The problem starts when i stop the app on Xcode and try to open it on my device without using Xcode, the app crashes on the splash screen. If i send any notification, it arrives with no issue, but when i open one notification the app just crashes on the splash screen again.
I have created all certificates and the development, production and ad-hoc provisioning profiles on my apple developer account, created and APNs Key to store on my Firebase Account, I have the file GoogleService-Info.plist on my Resources folders (platforms/ios/AppName/Resources & platforms/ios/AppName/Resources/Resources).
The only error that i can see on my log is this one
Unable to connect to FCM. Error Domain=com.google.fcm Code=2001 "FIRMessaging is already connected"
and this one
The object does not respond to -messaging:didReceiveRegistrationToken:, nor -messaging:didRefreshRegistrationToken:. Please implement -messaging:didReceiveRegistrationToken: to be provided with an FCM token.
This errors are shown when i accept the notification permission bubble.
Here is my JS for handling notifications:
function firebasePUSH() {
if (device.platform === "iOS") {
window.FirebasePlugin.hasPermission(function (data) {
if (!data.isEnabled) {
window.FirebasePlugin.grantPermission();
}
});
}
window.FirebasePlugin.onNotificationOpen(function (notification) {
console.log("tipo de notificacion " + notification.tipo);
console.log(notification);
if (notification.tipo === "alerta") {
var parametros = {
id: notification.numero,
categoria: "rescato"
};
myApp.request.post("http://190.98.210.41/comuna/app/contactos.php", parametros, function (data) {
var json = JSON.parse(data);
console.log(json);
if (json.error === false) {
mostrarSOS(json.alerta);
}
});
} else if (notification.tipo === "chat" || notification.tipo === "salud" || notification.tipo === "seguridad" || notification.tipo === "contacto" || notification.tipo === "oficina") {
aceptarLlamada();
} else if (notification.tipo === "publicidad") {
mostrarPublicidad(notification.numero);
} else if (notification.tipo === "sondeo") {
mostrarSondeo(notification.numero);
}
}, function (error) {
console.error("onResume>>" + error);
});
window.FirebasePlugin.getToken(function (token) {
try {
var jsonToken = JSON.parse(token);
token = jsonToken.token;
console.warn("venia json: " + jsonToken.token);
}
catch (err) {
console.warn("viene json limpio: " + token);
}
console.log("getToken js: " + token);
localStorage.setItem('registrationId', token);
/*PEGRUNTA SI YA ESTA LOGEADO*/
if (localStorage.getItem("correo") !== null && localStorage.getItem("clave") !== null) {
//pasa a la pantalla principal
var parametros = {
"id": localStorage.getItem("id"),
"token": localStorage.getItem('registrationId'),
"categoria": "token",
format: "json",
callback: function () {
return true;
}
};
myApp.request.json("http://190.98.210.41/comuna/app/usuario_get.php", parametros, function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
}
}, function (error) {
console.error("getToken error: " + error);
});
window.FirebasePlugin.onTokenRefresh(function (token) {
try {
var jsonToken = JSON.parse(token);
token = jsonToken.token;
console.warn("token json: " + jsonToken.token);
}
catch (err) {
console.warn("token limpio: " + token);
}
console.log("onTokenRefresh js: " + token);
localStorage.setItem('registrationId', token);
/*PEGRUNTA SI YA ESTA LOGEADO*/
if (localStorage.getItem("correo") != null && localStorage.getItem("clave") != null) {
//pasa a la pantalla principal
var parametros = {
"id": localStorage.getItem("id"),
"token": localStorage.getItem('registrationId'),
"categoria": "token",
format: "json",
callback: function () {
return true;
}
};
myApp.request.json("http://190.98.210.41/comuna/app/usuario_get.php", parametros, function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
}
}, function (error) {
console.error(error);
});
}
I could use some help on this issue because i have been on this for weeks and i'm getting so frustrated. Thank you very much guys.
EDIT:
I spotted a third error when i launch the app.
[Firebase/Messaging][I-FCM002023] The object does not respond to -messaging:didReceiveRegistrationToken:, nor -messaging:didRefreshRegistrationToken:. Please implement -messaging:didReceiveRegistrationToken: to be provided with an FCM token.
I had a problem very similar..
and I discovered that the problem was when i called window.FirebasePlugin.grantPermission() in second time.
do one test without asking permission more than once..

Cordova app with SQLite database freezes after first run in iOS but works fine in Android

I use SQlite in a Cordova app built with AngularJS. I bootstrap the app onDeviceReady() and then check if a database and a specific table exist and based on the result do certain things. The app works as expected in Android but in iOS it runs the fist time and then it gets blocked on the white page of the simulator or device! Can anyone give me an insight on how to resolve this issue?
var db = window.sqlitePlugin.openDatabase(
// options
{
name: "users_info.db",
location: 2
},
// success callback
function (msg) {
// console.log("success: " + msg);
// console.log(msg);
},
// error callback
function (msg) {
// console.log("error: " + msg);
}
);
db.transaction(function (tx) {
tx.executeSql("SELECT name FROM sqlite_master WHERE type='table' AND name='user'", [], function (tx, result) {
if (result.rows.length == 0) {
console.log('theres no table with this name ');
$scope.$apply(function () {
$location.path('/login');
});
} else {
tx.executeSql(
"select * from user;",
[],
function (tx, res) {
var row = res.rows.item(0);
console.log(row.role);
if (row.role == 4) {
$scope.$apply(function () {
userService.roleid = '4';
userService.userid = row.userid;
$location.path('/student');
});
}
});
console.log('this table exists');
}
});
});
I have also tried the following codes to open the database but again in iOS the app freezes after the first run
var db = $cordovaSQLite.openDB({ name: "users_info.db", location: 1, iosDatabaseLocation: 'default' },
// success callback
function (msg) {
// console.log("success: " + msg);
console.log(msg);
console.log('success');
},
// error callback
function (msg) {
console.log("error: " + msg);
console.log('error');
}
);
I found out that the reason of the issue was that my page containing the code snippet for the SQLite database refreshed twice when loading which caused the codes to read from and write to the SQlite database twice simultaneously which cause the database and consequently the iOS app to freeze. I made sure the codes run only once and it worked like a charm!

Is there support to Capture Video with Sound in phonegap (cordova)?

Is there support to Capture Video with Sound in phonegap (cordova)?
I checked on my iPad a phonegap 3.1 app whit Video Capture and no sound recording with the video
appreciate help
Thanks
function uploadFile(mediaFile) {
var ft = new FileTransfer(),
path = mediaFile.fullPath,
name = mediaFile.name;
ft.upload(path,
"http://www/api/up_video",
function(result) {
cordova.logger.log('Upload success: ' + result.responseCode);
cordova.logger.log(result.bytesSent + ' bytes sent');
},
function(error) {
cordova.logger.log('Error uploading file ' + path + ': ' + error.code);
},
{ fileName: name });
}
// capture callback
var captureSuccess = function(mediaFiles) {
var i, path, len;
for (i = 0, len = mediaFiles.length; i < len; i += 1) {
path = mediaFiles[i].fullPath;
name = mediaFiles[i].name;
// do something interesting with the file
alert(name);
//alert(mediaFiles[i].size);
//alert(mediaFiles[i].type);
uploadFile(mediaFiles[i]);
}
};
// capture error callback
var captureError = function(error) {
navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
cordova.logger.log( error);
};
navigator.device.capture.captureVideo(captureSuccess, captureError, {duration:6});
Absolutely. Both video and sound can be captured using Phonegap's "Capture" API. See documentation here.
Edit based on comments:
You are never telling the device to capture audio. The capture of Video and Audio are different operations. You need to also call the navigator.device.capture.captureAudio function. See here.

PhoneGap iOS - media.startRecord file URI not working

I am trying to use the audio recording as described in the PhoneGap API documentation. But on IOS there seems to be a problem with the file URI for myrecording.wav. I am using the following code to write to a file named myrecodring.wav
function recordAudio() {
var src = "myrecording.wav";
var mediaRec = new Media(src, onSuccess, onError);
// Record audio
mediaRec.startRecord();
// Stop recording after 10 sec
var recTime = 0;
var recInterval = setInterval(function() {
recTime = recTime + 1;
setAudioPosition(recTime + " sec");
if (recTime >= 10) {
clearInterval(recInterval);
mediaRec.stopRecord();
}
}, 1000);
}
i've also tried file://myrecording.wav and file:///myrecording.wav
i've done my test on ios5 iphone 4gs. I get the failure message with code 1 saying "Cannot use audio file from resource...."
Thanks for you help,
Martin
You have to first get file system:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
Then get the file:
function gotFS(fileSystem) {
alert('got FS');
fileSystem.root.getFile("myrecording4.wav", {create: true, exclusive: false}, gotFileEntry, fail);
}
Now you can get the file URI:
function gotFileEntry(fileEntry) {
alert('File URI: ' + fileEntry.toURI());
}

Resources