Media-Capture Recording Video and upload server


#1

I record video and select libary but not upload and watch…
video.html

Please Select Image!

Select Image Upload

video.ts
import { Component } from ‘@angular/core’;
import { IonicPage, NavController, NavParams, ActionSheetController, ToastController, Platform, LoadingController, Loading } from ‘ionic-angular’;
import { Camera } from ‘@ionic-native/camera’;
import { MediaCapture, MediaFile, CaptureError, CaptureImageOptions } from ‘@ionic-native/media-capture’;
import { File } from ‘@ionic-native/file’;
import { Transfer, TransferObject } from ‘@ionic-native/transfer’;
import { FilePath } from ‘@ionic-native/file-path’;
declare var cordova: any;
/**

  • Generated class for the VideoupPage page.
  • See http://ionicframework.com/docs/components/#navigation for more info
  • on Ionic pages and navigation.
    */
    @IonicPage()
    @Component({
    selector: ‘page-videoup’,
    templateUrl: ‘videoup.html’,
    providers:[MediaCapture]
    })
    export class VideoupPage {

lastImage: string;
loading: Loading;
constructor(public navCtrl: NavController, public navParams: NavParams,private camera: Camera, private transfer: Transfer,private mediaCapture: MediaCapture, private file: File, private filePath: FilePath, public actionSheetCtrl: ActionSheetController, public toastCtrl: ToastController, public platform: Platform, public loadingCtrl: LoadingController) {
}
public presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: ‘Select Video Source’,
buttons: [
{
text: ‘Load from Library’,
handler: () => {
this.takePicture(this.camera.MediaType.VIDEO);
}
},
{
text: ‘Use Video Camera’,
handler: () => {
this.mediaCapture.captureVideo().then(videodata=>{
this.lastImage=videodata.toString();

        })
      }
    },
    {
      text: 'Cancel',
      role: 'cancel'
    }
  ]
});
actionSheet.present();

}

public takePicture(sourceType) {
// Create options for the Camera Dialog
var options = {
sourceType: 2,
mediaType: 1,
saveToPhotoAlbum: true,
correctOrientation: true
};

// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
// Special handling for Android library
if (this.platform.is(‘android’) && sourceType === this.camera.MediaType.VIDEO) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf(’/’) + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf(’/’) + 1, imagePath.lastIndexOf(’?’));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
} else {
var currentName = imagePath.substr(imagePath.lastIndexOf(’/’) + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf(’/’) + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
}, (err) => {
this.presentToast(‘Error while selecting image.’);
});
}
private createFileName() {
var d = new Date(),
n = d.getTime(),
newFileName = n + “.mp4”;
return newFileName;
}

// Copy the image to a local folder
private copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
}, error => {
this.presentToast(‘Error while storing file.’);
});
}

private presentToast(text) {
let toast = this.toastCtrl.create({
message: text,
duration: 3000,
position: ‘top’
});
toast.present();
}

// Always get the accurate path to your apps folder
public pathForImage(video) {
if (video === null) {
return ‘’;
} else {
return cordova.file.dataDirectory + video;
}
}
public uploadImage() {
// Destination URL
var url = “http://bigg.akillisirketler.com/uploads.php”;

// File for Upload
var targetPath = this.pathForImage(this.lastImage);

// File name only
var filename = this.lastImage;

var options = {
fileKey: “file”,
fileName: filename,
chunkedMode: false,
mimeType: “video/mp4”,
params : {‘fileName’: filename}
};

const fileTransfer: TransferObject = this.transfer.create();

this.loading = this.loadingCtrl.create({
content: ‘Uploading…’,
});
this.loading.present();

// Use the FileTransfer to upload the image
fileTransfer.upload(targetPath, url, options).then(data => {
this.loading.dismissAll()
this.presentToast(‘Image succesful uploaded.’);
}, err => {
this.loading.dismissAll()
this.presentToast(‘Error while uploading file.’);
});
}

}