Cordova Media Plugin not working - ios

I can't get the cordova media plugin to work, I get error code 1 which signals that the file is not getting loaded. I've tried a number of variations, but can't figure out which path is correct.
Currently my code looks like this:
function onDeviceMediaReady () {
var path = window.cordova.file.applicationDirectory + 'why.mp3';
console.log(path);
narrative = new Media(path, // success callback
function() {
console.log("playAudio():Audio Success");
},
// error callback
function(err) {
console.log("playAudio():Audio Error: "+ err.code);
});
}
this gives me a path that's file://var/cotainers/Bundle/Application/[GUID]/Cordova400.app/why.mp3
I don't get why I can't find it. the file is in the telerik appbuilder root directory.

Inside your 'WWW' folder place the file inside 'sound' folder & try following
var srcBookmark = "sound/yes.mp3"; //ios
var iOSPlayOptions = {
numberOfLoops: 1,
playAudioWhenScreenIsLocked : false
}
var media = $cordovaMedia.newMedia(srcBookmark);
//media.play(); //android
media.play(iOSPlayOptions); //ios
$timeout(function(){
media.stop();
media.release()
}, 500);

If you're using appBuilder to test your app te aplication folder is the telerik folder, you must deploy your app as APK and test the folder

Related

Ionic - Some functionnalities are not working while compiling on IOS

Context:
Hello, I am working on a Ionic application (made in Typescript). In this application, I control a wifi camera via HTTP Request to the IP (http://192.72.1.1/myCommand to be precise).
Main actions I do with the camera:
Start recording
Stop recording
Get videos list
Download a video
When I use the Ionic DevApp:
With the Ionic DevApp, everything works perfectly, I can do all mains actions without a problem.
When I compile the application on IOS:
I compile with the command ionic cordova build ios --prod, then I archive with Xcode and send it to the AppStore to test it with Test Flight.
I got no errors while compiling / archive the application. But when I try it on my iPhone, I can start / stop recording, but can't download the video.
Problem:
Some commands are not working, but I don't know if it is getting the list or downloading the video, I have no logs. I don't understand why some commands are working but others no.
IOS is blocking download requests? How to solve my problem?
Notes:
I already tried all basic things like delete the IOS platform, recompile, uninstall, ...
I tried different Ionic HTTP plugins, same problem with all of them.
Some code:
Start / Stop the camera: (it is the same command to start / stop).
startCamera(){
var url = "http://192.72.1.1/changeRecordStatus";
var result = this.http.get(url);
result.subscribe(data => {
console.log("Works");
},
err => {
console.log("Error" + err);
}
);
}
Getting the name of the last video:
getLastVideo(){
var url = "http://192.72.1.1/listVideos";
this.http.get(url, {}, {})
.then(data => {
var xml = data.data
var xmlDOM = new DOMParser().parseFromString(xml, 'text/xml');
var temp = this.xmlToJson(xmlDOM); // function that convert XML to JSON
var resultArray = Object.keys(temp).map(function(i){
let ite = temp[i];
return ite;
});
resultArray = resultArray[0]['file'].reverse();
this.lastVideo = resultArray[0]['name']; // lastVideo is a global variable
},
(error) =>{
console.log("Error while getting the name of the last video" + error);
});
}
Downloading the file from the camera:
downloadFileFromCamera() {
this.getLastVideo();
var basename_file = this.lastVideo;
var url = "http://192.72.1.1" + basename_file;
this.fileTransfer.download(encodeURI(url), this.file.dataDirectory + '/videos/' + basename_file, true).then((entry) => {
this.video[this.counterVideos] = entry; // video is a global array
this.counterVideos +=1;
}, (error) => {
console.log("Error while downloading the last video" + error);
});
}
If someone knows how to solve my problem, I would be so grateful! Thanks in advance.

How to download picture with Phonegap Filetransfer to image gallery in iOS

This has been asked several times, but most of these questions are unanswered
I'm downloading a file like below, and it seems to work fine. However .. it does not show up in the iOS, ehm, gallery. That is, in the 'photos' application.
var fileTransfer = new FileTransfer();
var encurl = encodeURI(url);
var filename = url.split('/').slice(-1)[0];
var filepath = "foo/"+filename;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
var syspath = fileSystem.root.toURL() + '/' + filepath;
var fileTransfer = new FileTransfer();
fileTransfer.download(
encurl,
syspath,
function (entry) {
foo.debug("Download success "+syspath);
},
function (error) {
foo.error("Download failed with error "+error.code+' '+syspath);
}
);
}, function (evt) {
foo.error("Filesystem failed with error "+evt.target.error.code);
});
and the result is
[Log] Download success file:///var/mobile/Applications/68DE0AD9-FBD2-4D82-92C0-2B7634B218D5/Documents//foo/20141030-153810-editor.jpg (console-via-logger.js, line 173)
hurray. now, how do you open the download, using just your fingers, on ios ?
I would mark this as a duplicate of Phonegap - Save image from url into device photo gallery
I was happy with the answer by M165437 and my comments. That answered my question.
Try to download by using https://www.npmjs.com/package/com-cordova-image-save-to-gallery plugin. It will download a picture from a given URL and save it to IOS Photo Gallery.
Cordova plugin add https://github.com/valwinjose007/cordova-image-save-to-gallery.git
How to use:
declare var CordovaImageSaveToGallery: any;
CordovaImageSaveToGallery.downloadFromUrl('https://picsum.photos/200/300',(res)=>{
//download success
},(err)=>{
//error on download
});

File Transfer Download with Cordova 3.4 for iOS 7

I'm a little confused with the filetransfer method of Cordova for iOS. (I had a version working with Android)
Apparently, I don't set the destination folder properly. target:null Could not create target file Note that I assume that the directory exists as it is created with success earlier in the script.
According to the Cordova documentation, I should use a entry.toURL to get the right path.
function download(filename){
var localPath = rootFS.toURL+'contentImages/'+filename;
var fileTransfer = new FileTransfer();
fileTransfer.download(encodeURI('http://myValidatedSource.com/'+filename),
localpath,
function(entry){
console.log('download completed for '+entry.fullPath);
},
function(error){
console.log(error);
}
);
}
I also tried this:
alert(rootFS.fullPath); ==> "/"
and
alert(rootFS.toURL); ==> "function(){
if (this.nativeURL){
return this.nativeURL;
}
return this.toInternalURL()|| "file://localhost"+this.fullPath";
}"
I was not so far...
var localPath = rootFS().toURL+'contentImages/'+filename;
instead of
var localPath = rootFS.toURL+'contentImages/'+filename;

Set folder metadata doesn't work on Phonegap 3.3.0 / iOS

I'm trying to use the method setMetadata, using the File plugin, but it seems does not work.
No Success or Fail callback is executed. When I use console.log(entry.setMetadata), it prints the correct method. I use the File plugin to access, create and delete files and folders without problems. Only the setMetadata doesn't work.
Example:
localFileSystem = LocalFileSystem.PERSISTENT;
subFolder = "Backups";
metadataKey = "com.apple.MobileBackup";
metadataValue = 1;
window.requestFileSystem(localFileSystem, 0, function(fileSystem) {
fileSystem.root.getDirectory(subFolder, {create: true, exclusive: false}, function(parent) {
var data = {};
data[metadataKey] = metadataValue;
console.log(data); // OK
console.log(parent); // OK
parent.setMetadata(function() {
console.log("success setting metadata"); // Nothing
}, function() {
console.log("error setting metadata"); // Nothing
}, data);
}, function() {
console.log("error getting dir"); // Nothing, directory is OK
});
}, function(error) {
console.log(error.code); // No error here
});
It was a bug on the File plugin. I checked with the developers on Github:
https://github.com/apache/cordova-plugin-file/pull/39
Just waiting for changes on the Phonegap site.

Cordova iOS File API - Move Photo to a persistent storage

I've a problem with moving a camera photo to the persistent storage under iOS 7 (Cordova 3.4.0-0.1.3 - File API 1.0.1).
I can capture the photo and when I move the file to the persistent storage it seems that there is no error, I also receive a file path with new_entry.fullPath like /my_folder/12345678.jpg.
But when I append the new image to the body with that url it seems that there is no image (blank image will be added). I've tried it also with "file://" in the url, but this makes no difference.
I'm also a little bit confused, because the new_entry.toURL() method returned an url containing a folder named "temporary" (e.g. cdvfile://localhost/temporary/my_folder/12345678.jpg), but I use the persistent storage. Is that correct under iOS?
This is my relevant code for that function:
var app = {
capturePhoto: function () {
if (!navigator.camera) {
alert('Camera API not supported');
}
navigator.camera.getPicture( app.cameraSuccess, app.cameraError, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI
});
},
cameraSuccess: function (imageData) {
console.log('cameraSuccess: '+imageData);
app.movePhoto( imageData );
},
movePhoto: function (file){
alert(file);
window.resolveLocalFileSystemURI( file , app.resolveOnSuccess, app.resOnError);
},
resolveOnSuccess: function (entry){
var d = new Date();
var n = d.getTime();
//new file name
var newFileName = n + ".jpg";
var myFolderApp = "my_folder";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
fileSys.root.getDirectory( myFolderApp,
{create:true},
function(directory) {
entry.moveTo(directory, newFileName, function(new_entry){
path = new_entry.fullPath;
url = new_entry.toURL();
console.log(path+"\n"+url);
alert( path+"\n"+url );
jQuery('body').append('<img src="'+path+'" />');
}, app.resOnError);
},
app.resOnError);
},
app.resOnError);
},
resOnError: function(error) {
alert('Error '+error.code+': '+error.message);
},
}
27/5/2014 UPDATE: Version 1.1.0 was released since than, therefore no need to use dev branch anymore.
It's a bug in cordova: https://issues.apache.org/jira/browse/CB-6148
It's already fixed in dev branch. You can update to dev branch with those steps:
remove the plugin:
cordova plugin rm org.apache.cordova.file
install the plugin (we have to use the git syntax in this case):
cordova plugin add https://github.com/apache/cordova-plugin-file.git#dev
check the iOS build > Targets > Your app target > Build phases > Compile Sources
add (if not added)
CDVFile.m
CDVLocalFilesystem.m
CDVAssetLibraryFilesystem.m

Resources