I am using Ionic 3 and I used the native plugin called Ionic Native - Media and the Ionic File. I am successfully recording an audio using my mobile devices in android and the play button is working perfectly. However I wanted to get the duration of the file of the audio and display the total length of the duration. I also want to display the current duration when the button is click.
Here is my code in my home.html
<ion-header>
<ion-navbar>
<ion-title>
Sound Recorder & Player
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-card>
<ion-card-content>
<ion-card-title>
<button ion-button primary (click)="stopRecord()" *ngIf="recording"><ion-icon name="mic-off"></ion-icon> Stop Record</button>
<button ion-button primary (click)="startRecord()" *ngIf="!recording"><ion-icon name="mic"></ion-icon> Start Record</button>
</ion-card-title>
</ion-card-content>
</ion-card>
<ion-list>
<ion-item *ngFor="let audio of audioList; index as i;">
<p>{{audio.filename}}</p>
<button ion-button clear item-end large (click)="playAudio(audio.filename, i)"><ion-icon name="play"></ion-icon></button>
</ion-item>
</ion-list>
</ion-content>
and here is the code in my home.ts
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { Media, MediaObject } from '@ionic-native/media';
import { File } from '@ionic-native/file';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
recording: boolean = false;
filePath: string;
fileName: string;
audio: MediaObject;
audioList: any[] = [];
constructor(
public navCtrl: NavController,
private media: Media,
private file: File,
public platform: Platform
) {
}
ionViewWillEnter() {
this.getAudioList();
}
getAudioList() {
if(localStorage.getItem("audiolist")) {
this.audioList = JSON.parse(localStorage.getItem("audiolist"));
console.log(this.audioList);
}
}
startRecord() {
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.documentsDirectory.replace(/file:\/\//g, '') + 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.externalDataDirectory.replace(/file:\/\//g, '') + this.fileName;
this.audio = this.media.create(this.filePath);
let duration = this.audio.getDuration();
console.log(duration)
}
this.audio.startRecord();
this.recording = true;
}
stopRecord() {
this.audio.stopRecord();
let duration = this.audio.getDuration()
console.log(duration)
let data = { filename: this.fileName };
this.audioList.push(data);
localStorage.setItem("audiolist", JSON.stringify(this.audioList));
this.recording = false;
this.getAudioList();
}
playAudio(file,idx) {
if (this.platform.is('ios')) {
this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + file;
this.audio = this.media.create(this.filePath);
} else if (this.platform.is('android')) {
this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '') + file;
this.audio = this.media.create(this.filePath);
let duration = this.audio.getDuration()
console.log('Duration:', duration)
}
this.audio.play();
this.audio.setVolume(1.0);
}
}
I tried to console.log the getDuration() function that is written in the docs of Media. However it always return me -1
Any thoughts here?
Appreciate if someone could help. Thanks in advance.