I'm trying to record audio inside ionic 4 app using media-capture cordova plugin, But the problem is when I hit the record button it opens an external recorder app and records the audio, then I can play it inside my app. I would like to record the audio inside my app itself without opening an external recorder. Please help!
Here's my home.page.html:
<ion-header>
<ion-toolbar>
<ion-title>
Audio Testing App
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<div class="ion-padding">
<ion-button color="primary" (click)="record()">Record</ion-button>
<ion-list>
<ion-item-sliding *ngFor="let f of files">
<ion-item (click)="openFile(f)">
<ion-icon name="mic" slot="start"></ion-icon>
<ion-label class="ion-text-wrap">
{{ f.name }}
<p>{{ f.fullPath }}</p>
</ion-label>
</ion-item>
<ion-item-options side="start">
<ion-item-option (click)="deleteFile(f)" color="danger">
<ion-icon name="trash" slot="icon-only"></ion-icon>
</ion-item-option>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</div>
</ion-content>
home.page.ts file:
import { Component, OnInit } from '@angular/core';
import { NativeAudio } from '@ionic-native/native-audio/ngx';
import { MediaCapture, MediaFile, CaptureError } from '@ionic-native/media-capture/ngx';
import { File, FileEntry } from '@ionic-native/File/ngx';
import { Platform } from '@ionic/angular';
import { Media, MediaObject } from '@ionic-native/media/ngx';
const MEDIA_FOLDER_NAME = 'audio_app_media';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
files = [];
constructor(private nativeAudio: NativeAudio,
private mediaCapture: MediaCapture,
private file: File,
private media: Media,
private plt: Platform) { }
ngOnInit() {
this.plt.ready().then(() => {
let path = this.file.dataDirectory;
this.file.checkDir(path, MEDIA_FOLDER_NAME).then(
() => {
this.loadFiles();
},
err => {
this.file.createDir(path, MEDIA_FOLDER_NAME, false);
}
);
});
}
loadFiles() {
this.file.listDir(this.file.dataDirectory, MEDIA_FOLDER_NAME).then(
res => {
this.files = res;
},
err => console.log('error loading files: ', err)
);
}
record() {
this.mediaCapture.captureAudio().then(
(data: MediaFile[]) => {
if (data.length > 0) {
this.copyFileToLocalDir(data[0].fullPath);
}
},
(err: CaptureError) => console.error(err)
);
}
openFile(f: FileEntry) {
const path = f.nativeURL.replace(/^file:\/\//, '');
const audioFile: MediaObject = this.media.create(path);
audioFile.play();
}
deleteFile(f: FileEntry) {
const path = f.nativeURL.substr(0, f.nativeURL.lastIndexOf('/') + 1);
this.file.removeFile(path, f.name).then(() => {
this.loadFiles();
}, err => console.log('error remove: ', err));
}
copyFileToLocalDir(fullPath) {
let myPath = fullPath;
// Make sure we copy from the right location
if (fullPath.indexOf('file://') < 0) {
myPath = 'file://' + fullPath;
}
const ext = myPath.split('.').pop();
const d = Date.now();
const newName = `${d}.${ext}`;
const name = myPath.substr(myPath.lastIndexOf('/') + 1);
const copyFrom = myPath.substr(0, myPath.lastIndexOf('/') + 1);
const copyTo = this.file.dataDirectory + MEDIA_FOLDER_NAME;
this.file.copyFile(copyFrom, name, copyTo, newName).then(
success => {
this.loadFiles();
},
error => {
console.log('error: ', error);
}
);
}
}
Here's the screenshots of the app: Image 1: home page
Image 2: External recorded, on clicking record button this opens up
(DON'T WANT TO GO HERE, INSTEAD RECORD DIRECTLY INSIDE MY APP)
You should try cordova-plugin-media.