Hello,
I’m trying to create a voice recorder using Ionic-native Media plugin.
I’m able to start a recording, pause it, listen it and finish the recording. However, I would like to be able to pause it, listen from the beggining and then resume the recording. Another feature is to resume the recording from a certain point.
Is this possible? Can you help me?
This is what I have. The code is not clean as I’m trying a lot of stuff (sry for the mess)
import { Media, MediaObject } from '@ionic-native/media/ngx';
import { Component, OnInit, ViewChild } from '@angular/core';
import { ToastController } from '@ionic/angular';
import { Plugins, FilesystemDirectory, FilesystemEncoding } from '@capacitor/core';
import { File } from '@ionic-native/file/ngx';
import { Storage } from '@ionic/storage';
import { WebView } from '@ionic-native/ionic-webview/ngx';
import { Observable, Subscription } from 'rxjs';
import { timer } from "rxjs";
@Component({
selector: 'app-create-process',
templateUrl: './create-process.page.html',
styleUrls: ['./create-process.page.scss'],
})
export class CreateProcessPage implements OnInit {
skipTime: number = 2; //2seg
currentTime: number;
totalTime: number;
isListening: boolean;
isRecording: boolean;
isPlaying: boolean;
audio: MediaObject;
playBackAudio: MediaObject;
time: number;
timerSubscription: Subscription;
constructor(
private media: Media,
public toastController: ToastController,
private storage: Storage,
private webview: WebView,
private file: File) {
}
ngOnInit() {
this.isListening = false;
this.isRecording = false;
this.currentTime = this.totalTime = 0;
this.audio = undefined;
this.isPlaying = false;
this.time = 0;
this.timerSubscription = timer(0, 1000).subscribe(ellapsedCycles => {
if (this.isRecording) {
this.time += 1;
}
});
}
GetTimeFromSeconds(time: number): string {
var minutes = Math.floor(time / 60);
var seconds = time - minutes * 60;
return minutes + " : " + Math.floor(seconds);
}
Record() {
this.isRecording = true;
this.isListening = false;
this.isPlaying = false;
if (this.audio == undefined) {
this.audio = this.media.create('test.mp3');
this.audio.onError.subscribe(error => { console.log('-- ERROR! --- '); console.log(error); });
this.audio.onStatusUpdate.subscribe(
status => {
console.log("STATUS : " + status);
if (status == 4){ //PAUSED OR ENDED
this.isPlaying = false;
}
}
);
this.audio.startRecord();
console.log("RECORDING");
}
else {
this.audio.resumeRecord();
console.log("BACK");
this.playBackAudio = undefined;
}
this.totalTime = this.audio.getDuration();
}
Pause() {
this.isRecording = false;
this.audio.pauseRecord();
console.log("PAUSA");
if (this.audio == undefined) {
console.log("UNDIFINED");
}
else {
console.log("NICE ;)");
}
if (!this.playBackAudio) {
this.playBackAudio = this.audio;
this.playBackAudio.stopRecord();
}
}
PauseListening() {
this.playBackAudio.stop();
this.isPlaying = false;
this.playBackAudio.getCurrentPosition().then(
(actualPos) => {
if (actualPos < 0) {
console.log("actualPos < 0");
actualPos = 0;
}
this.currentTime = (actualPos);
console.log("ACTUAL POS : " + actualPos);
console.log("CURRENT TIME : " + this.currentTime);
if (this.currentTime < 0) {
this.currentTime = 0;
}
console.log("PAUSED AT " + this.currentTime);
}
);
}
Play() {
this.playBackAudio.getCurrentPosition().then(
(actualPos) => {
if (actualPos < 0) {
actualPos = 0;
}
this.currentTime = Math.floor(actualPos);
console.log("ACTUAL POS : " + actualPos);
console.log("CURRENT TIME : " + this.currentTime);
if (this.currentTime < 0) {
this.currentTime = 0;
}
console.log("PLAYING FROM :: " + this.currentTime * 1000);
this.playBackAudio.seekTo(Math.floor(this.currentTime * 1000));
this.isListening = true;
this.isPlaying = true;
this.playBackAudio.play();
console.log("PLAY");
}
);
}
Finish() {
this.audio.stopRecord();
this.audio.release();
this.audio = undefined;
console.log("STOP");
this.timerSubscription.unsubscribe();
this.ngOnInit();
}
PlayBack() {
this.playBackAudio.getCurrentPosition().then(
(actualPos) => {
if (actualPos < 0) {
actualPos = 0;
}
this.currentTime = actualPos;
console.log("ACTUAL POS : " + actualPos);
console.log("CURRENT TIME : " + this.currentTime);
if (this.currentTime > 0) {
if (this.currentTime - this.skipTime < 0) {
console.log("this.currentTime = 0;");
this.currentTime = 0;
}
else {
this.currentTime -= this.skipTime;
}
}
else {
this.currentTime = 0;
}
this.playBackAudio.stop();
this.Play();
}
);
}