验证中...
8月18日(周六)成都源创会火热报名中,四位一线行业大牛与你面对面,探讨区块链技术热潮下的冷思考。
片段 1 片段 2
home.ts
原始数据 复制代码
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { MediaCapture, MediaFile, CaptureError, CaptureAudioOptions } from '@ionic-native/media-capture';
import { Media, MediaObject } from '@ionic-native/media';
import { FileTransfer, FileUploadOptions, FileTransferObject, FileUploadResult } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
declare var cordova: any;
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [MediaCapture, Media, FileTransfer, FileTransferObject, File]
})
export class HomePage {
storageDirectory: string = '';
fileName: string = '';
constructor(public navCtrl: NavController, public platform: Platform,
public mediaCapture: MediaCapture, public media: Media, public ft: FileTransfer, private file: File) {
this.platform.ready().then(() => {
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if (!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = cordova.file.tempDirectory;
}
else if (this.platform.is('android')) {
this.storageDirectory = cordova.file.dataDirectory;
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
});
}
// cordova-plugin-media-capture 的使用
startRecording_MediaCapture() {
this.platform.ready().then(() => {
const ftObj: FileTransferObject = this.ft.create();
// 设置录音参数:duration限制录音长度,单位秒,仅ios有效
let options: CaptureAudioOptions = { limit: 1, duration: 10};
this.mediaCapture.captureAudio(options)
.then(
function (mediaFiles: MediaFile[]) {
var i, len;
for (i = 0, len = mediaFiles.length; i < len; i += 1) {
alert("Record success! \n\n"
+ "file name: " + mediaFiles[i].name + "\n\n"
+ "size: " + (mediaFiles[i].size / 1024).toFixed(2) + "KB" + "\n\n"
+ "fullPath: " + mediaFiles[i].fullPath + "\n\n"
+ "lastModifiedDate: " + (mediaFiles[i].lastModifiedDate) + "\n\n"
+ "type: " + mediaFiles[i].type + "\n\n");
// 上传
this.fileName = mediaFiles[i].name;
this.uploadFile(mediaFiles[i].fullPath);
}
},
);
});
}
// cordova-plugin-media 的使用
startRecording_Media() {
this.platform.ready().then(() => {
let mediaObj;
var recordName = this.generateFileName() + ".wav";
this.fileName = recordName;
if (this.platform.is('ios')) {
this.file.createFile(this.file.documentsDirectory, recordName, true).then(() => {
mediaObj = this.media.create(this.file.documentsDirectory.replace(/^file:\/\//, '') + recordName);
this.doRecord_Media(mediaObj);
});
} else if (this.platform.is('android')) {
mediaObj = this.media.create(recordName);
this.doRecord_Media(mediaObj);
} else {
alert("Not cordova!");
return;
}
});
}
doRecord_Media(mediaObj: MediaObject) {
// 开始录音
mediaObj.startRecord();
// 监测录音状态的回调
mediaObj.onStatusUpdate.subscribe(status => this.showRecordStatus(status));
// 录音成功后的处理,如上传录音
mediaObj.onSuccess.subscribe(() => this.uploadFile(this.file.documentsDirectory.replace(/^file:\/\//, '') + this.fileName));
// 录音失败后的处理,如提示错误码
mediaObj.onError.subscribe(error => alert('Record fail! Error: ' + error));
// 设置录音的长度,单位毫秒,ios / android 均有效
window.setTimeout(() => mediaObj.stopRecord(), 10 * 1000);
}
// 上传 File Transfer Plugin 的使用
uploadFile(filePath) {
// 设置上传参数
let options: FileUploadOptions = {
fileKey: "file",
fileName: this.fileName,
mimeType: "audio/wav"
};
const ftObj: FileTransferObject = this.ft.create();
alert("filePath:" + filePath);
ftObj.upload(filePath,
encodeURI("http://ec2-52-87-177-182.compute.amazonaws.com/upload.php"), options).then(
(data) => {
alert("File upload success!");
},
(err) => {
alert("File upload fail! error:" + err.code);
});
}
// 生成录音文件名的方法:yyyymmddhhmmss(月和日不足两位补0)
complement(n) { return n < 10 ? '0' + n : n }
generateFileName() {
var date = new Date();
return date.getFullYear().toString() + this.complement(date.getMonth() + 1) + this.complement(date.getDate()) + this.complement(date.getHours()) + this.complement(date.getMinutes()) + this.complement(date.getSeconds());
}
// 根据录音状态码返回录音状态的方法
showRecordStatus(status) {
var statusStr = "";
switch (status) {
case 0:
statusStr = "None";
break;
case 1:
statusStr = "Start";
break;
case 2:
statusStr = "Running";
break;
case 3:
statusStr = "Paused";
break;
case 4:
statusStr = "Stopped";
break;
default:
statusStr = "None";
}
alert("status: " + statusStr);
}
}
upload.php
原始数据 复制代码
<?php
header('Access-Control-Allow-Origin: *');
if(move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]))
{
echo 'upload success!!!';
} else {
echo 'upload fail!!!';
}
?>

评论列表( 0 )

你可以在登录后,发表评论