在我在Ionic 3上的应用程序中,我使用来自'@ ionic-native / media'的Media和MediaObject来录制音频并且这有效,音频被录制和播放,但在我录制一个音频后,MediaObject.startRecording不返回错误并创建音频文件,但是当我播放文件时,没有音频正在播放。 下面是我的.ts代码。
//This is my variables:
private recording: boolean = false;
private recordingIco: string = 'assets/img/micico.png';
private filePath: string;
private fileName: string;
private audio: MediaObject;
audioList: any[] = [];
public recordingMessage = "Wating for records..."
public MEDIA_FILES_KEY = 'mediaFiles';
private audioURI:any;
private audioFileName:any;
private isPlay:boolean = false;
private audioIco: string = 'assets/img/playico.png';
//This is my constructor:
constructor(public navCtrl: NavController, public navParams: NavParams, public http: Http, private media: Media, private file: File, public platform: Platform, private mediaCapture: MediaCapture, private storage: Storage, private transfer: FileTransfer) {}
//This is my rec and play functions:
startRecord() {
let audioRec:MediaObject;
this.recordingMessage = "Device is preparing to record...";
if (this.platform.is('ios')) {
this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new Date().getSeconds()+'.3gp';
this.filePath = this.file.dataDirectory + this.fileName;
this.audio = this.media.create(this.filePath);
} else if (this.platform.is('android')) {
this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new Date().getSeconds()+'.3gp';
this.filePath = this.file.dataDirectory + this.fileName;
this.audio = this.media.create(this.filePath);
console.log("recording file = " + this.filePath);
}
console.log("start recording...");
this.audio.startRecord();
console.log("recording is started...");
this.recordingMessage = "Device is recording...";
this.recording = true;
}
stopRecord() {
this.audio.stopRecord();
console.log("Recording is stoped...");
this.recordingMessage = "Recording is ended...";
let data = { filename: this.fileName };
this.audioList.push(data);
localStorage.setItem("audiolist", JSON.stringify(this.audioList));
this.recording = false;
}
playAudio() {
if (this.platform.is('ios')) {
this.filePath = this.file.dataDirectory + this.fileName;
this.audio = this.media.create(this.filePath);
} else if (this.platform.is('android')) {
this.filePath = this.file.dataDirectory + this.fileName;
this.audio = this.media.create(this.filePath);
}
this.audio.play();
this.audio.setVolume(0.8);
console.log("The device is playing audio...");
this.recordingMessage = "Device is playing audio...";
}