I am developing ionic 3 app that download videos from server to the device's native storage . i used this URL to save file:
let externalDir = this.file.externalDataDirectory;
file download is fine it is downloaded and i can view it from the device.
for more details the file link in the storage is:
/storage/emulated/0/Android/data/xxxxxxxx/files/VideoTest1/0.mp4
but the problem is i want to play that downloaded video into my app using <video>
tag, i used the same link to view it but it doesn't work. any help?
sorry for my english
I've been searching for a solution. I resolve this issue as follows.
After platform.ready()
function I created a directory as follows.
this.file.createDir(this.file.externalDataDirectory , 'Videos', false).then(() => {
// console.log('Directory created successfully');
}).catch(() => {
// console.log('Failed to create directory');
});
To download videos inside the created directory
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
import { Diagnostic } from '@ionic-native/diagnostic';
...
constructor(private diagnostic: Diagnostic,
private transfer: FileTransfer, private file: File)
download(item) {
let downloadProgress: any;
const fileTransfer: FileTransferObject = this.transfer.create();
const url = encodeURI(item.url);
const targetPath = this.file.externalDataDirectory + 'Videos/' + item.title + '.mp4';
this.diagnostic.requestCameraAuthorization(true).then(() => {
fileTransfer.download(url, targetPath, true).then((entry) => {
// console.log('download complete: ' + entry.toURL());
}, (error) => {
console.log(error);
});
fileTransfer.onProgress((progress) => {
downloadProgress = Math.round((progress.loaded / progress.total) * 100) + '%';
console.log('Downloading progress ...', downloadProgress);
});
}, (error) => {
console.log(error);
});
}
That downloads a file inside the created Videos
folder. You can actually find that on the device by going Android/data/com.your.app/files/
.
If you're using android
and that didn't work, add file.setReadable(true, false);
inside FileTransfer.java
.
To play the videos for offline usage
Make sure these lines exist in index.html
<meta http-equiv="Content-Security-Policy" content="style-src 'self' 'unsafe-inline'; media-src *; connect-src *">
And in your config.xml
file
<access origin="*" />
<allow-navigation href="*" />
<allow-navigation href="http://*/*" />
<allow-navigation href="file://*/*" />
<access origin="cdvfile://*" />
<allow-intent href="*" />
<access origin="*" />
If not add them.
Then finally in your video player page
<video id="video" controls="controls" preload="metadata" autoplay="autoplay" webkit-playsinline="webkit-playsinline" class="videoPlayer">
<source [src]="content" type="video/mp4" />
</video>
And in the component
ionViewDidEnter() {
this.file.listDir(this.file.externalDataDirectory, 'Videos')
.then((data) => {
console.log(data[0]);
this.count = data.length;
const src = data[0].toInternalURL();
this.file.resolveLocalFilesystemUrl(src).then(data => {
this.content = data.toURL();
document.getElementById('video').setAttribute('src', this.content);
console.log('Content path cahnged ', this.content);
}, error => {
console.log('File path error');
})
})
.catch(err => console.log('Directory doesnt exist'));
}
And that's it. I hope that works.
If you want to access any file for native storage of mobile. Then you have to create a local server of the folder you want the access of.
https://ionicframework.com/docs/native/httpd
constructor(public file:File, public ngZone : NgZone, public httpd :Httpd){
}
startLocalServe(){
let options: HttpdOptions = {
www_root: this.file.externalDataDirectory.replace('file://',''),
port: 9000,
localhost_only: true //if you want to create multiple localhost then false
};
this.httpd.startServer(options).subscribe(url => {
console.log('Server is live',url); //Server is live http://127.0.0.0.1:9000
this.ngZone.run(()=>{
this.localServeUrl = url;
})
},error => {
console.log('Fail to start Server : ',JSON.stringify(error));
});
}
Now you will able to localserveUrl as src="http://127.0.0.0.1:9000/VideoTest1/0.mp4"
put the code startLocalServe in app.component.ts
this.platform.ready().then(()=>{
startLocalServe();
});