我在Ionic 2应用程序中使用cordova-plugin-media-with-compression。
在iOS上,如果我传递startRecord()
文件名并再次调用,而不更改this.media
,我就可以录制和播放。
我似乎无法播放存储在文件系统其他位置的音频文件 - 因为我必须将新的src传递给startRecord()
,这就是我认为我做错了。
import { Component } from '@angular/core';
import { ModalController, LoadingController, ToastController, Platform } from 'ionic-angular';
import { File, FileEntry, Entry, FileError, DirectoryEntry} from 'ionic-native';
declare var Media: any; // stops errors w/ cordova-plugin-media-with-compression types
@Component({
selector: 'page-add-doc',
templateUrl: 'add-doc.html'
})
export class AddDocPage {
isRecording = false;
isRecorded = false;
audioUrl ='';
localAudioUrl = '';
media: any;
newFileName: string;
newFileNameM4A: string;
homerAudio = 'http://techslides.com/demos/samples/sample.m4a'
constructor(private modalCtrl: ModalController,
private loadingCtrl: LoadingController,
private toastCtrl: ToastController,
private platform: Platform,
) {
platform.ready()
.then(() => {
console.log('Platform Ready');
});
}
ionViewDidLoad() {
this.newFileName = new Date().getTime().toString();
this.newFileNameM4A = this.newFileName +'.m4a';
}
onRecordAudio() {
this.media = new Media(this.newFileNameM4A);
this.media.startRecord();
this.isRecording = true;
}
onStopRecordAudio() {
this.media.stopRecord();
this.media.release();
this.isRecording = false;
this.isRecorded = true;
try {
File.copyFile(File.tempDirectory, this.newFileNameM4A, File.dataDirectory, this.newFileNameM4A)
.then(
(data: Entry) => {
this.audioUrl = data.nativeURL;
});
} catch (FileError) {
console.log(FileError)
};
}
onPlayback() {
this.media = new Media(this.newFileNameM4A);
this.media.play();
this.media.release();
}
onPlaybackTempDirectory() {
this.media = new Media(File.tempDirectory + this.newFileNameM4A);
this.media.play();
this.media.release();
}
onPlaybackDataDirectory() {
this.media = new Media(File.dataDirectory + this.localAudioUrl);
this.media.play();
this.media.release();
}
onHomerAudio() {
this.media = new Media(this.homerAudio)
this.media.play();
this.media.release();
}
}