这是我的代码:
import {File,IWriteOptions} from '@ionic-native/file';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { Media, MediaObject } from '@ionic-native/media';
curr_playing_file: MediaObject;
constructor(public _zone: NgZone,
public databaseService: DatabaseService,
public socketService: SocketService,
public params:NavParams,
private platform:Platform,
public viewCtrl:ViewController,
private media:Media,
private transfer:FileTransfer,
private file:File) {
}
start()
{
this.curr_playing_file = this.createAudioFile('audio1.amr');
this.curr_playing_file.startRecord();
}
end()
{
this.curr_playing_file.stopRecord();
this.curr_playing_file.release();
console.log('released');
//SET NAME
var name = this.getRandomStr(); //get random string
let option: FileUploadOptions = {
fileKey:'file',
mimeType:'audio/amr',
httpMethod:'POST',
fileName:name
}
const fileTransfer:FileTransferObject = this.transfer.create();
fileTransfer.upload(this.curfilename, encodeURI(localStorage.getItem('GlobalIP')+"/upload"),option).then((result)=>
{
this.sendAudio(name);
alert('success');
}
).catch(error=>{
console.log('error');
});
}
createAudioFile(filename): MediaObject {
if (this.platform.is('ios')) { //ios
console.log(filename.replace(/^file:\/\//, ''));
return this.media.create(filename.replace(/^file:\/\//, ''));
} else { // android
return this.media.create(filename);
}
}
此代码适用于IOS,但不适用于Android。 我收到以下错误消息:
09-11 19:27:26.313 14361-14505 / com.ionicframework.exampleproject223738
E /文件传输:
{ “代码”:1, “源”: “audio3.amr”, “目标”: “http://183.207.183.247:8080/upload”, “HTTP_STATUS”:200, “异常”:“/audio3.amr
(没有这样的文件或目录)“} java.io.FileNotFoundException:
/audio3.amr(没有这样的文件或目录)
at java.io.FileInputStream.open(Native Method)
在java.io.FileInputStream。(FileInputStream.java:146)
在java.io.FileInputStream。(FileInputStream.java:99)
在org.apache.cordova.CordovaResourceApi.openForRead(CordovaResourceApi.java:249)
在org.apache.cordova.CordovaResourceApi.openForRead(CordovaResourceApi.java:232)
在org.apache.cordova.filetransfer.FileTransfer $ 1.run(FileTransfer.java:419)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607)
在java.lang.Thread.run(Thread.java:776)
答案 0 :(得分:0)
curr_playing_file: MediaObject;
recStart:boolean;
pressed()
{
console.log('Longpress');
//voiceNum INCREASE
var nItem = localStorage.getItem('recordvoiceNum');
var numstr = 0;
if(nItem == null){
numstr = 1;
}
else{
var numstr = parseInt(nItem,10)
numstr = numstr + 1;
}
//Create media file
this.curfilename = 'audio' + numstr + '.3gp';
this.curr_playing_file = this.createAudioFile(this.curfilename);
localStorage.setItem('recordvoiceNum', numstr.toString());
try {
console.log('start Recording');
if(this.recStart == false){
this.curr_playing_file.startRecord();
this.recStart = true;
}
}catch(e)
{
console.log('record error');
console.log(e.message);
}
}
released()
{
setTimeout(() => {
try{
console.log('stop recording');
//stop recording
this.curr_playing_file.stopRecord();
this.curr_playing_file.release();
console.log('released');
var name = this.getRandomStr();
let option: FileUploadOptions = {
fileKey:'file',
mimeType:'audio/3gp',
httpMethod:'POST',
fileName:'audiochat#'+name
}
const fileTransfer:FileTransferObject = this.transfer.create();
console.log('filename'+this.curfilename);
fileTransfer.upload(this.file.externalRootDirectory+this.curfilename, encodeURI(localStorage.getItem('GlobalIP')+"/upload"),option).then((result)=>
{
this.sendAudio(name);
this.recStart = false;
}
).catch(error=>{
console.log('uploaderror');
console.log(error.message);
this.recStart = false;
});
}
catch(error)
{
console.log('stoperror');
console.log(error.message);
}
}, 500);
}