I have a problem with "mediaplugin" in voice record

i have a problem with “mediaplugin” in voice record.

my error is
typeerror: cannot read property ‘startRecord’ of undefined.

my provider

import { Injectable } from ‘@angular/core’;
import { MediaPlugin } from ‘ionic-native’;

export enum AudioRecorderState {
Ready,
Recording,
Recorded,
Playing
}

@Injectable()
export class AudioRecorder {
mediaPlugin: MediaPlugin = null;
state: AudioRecorderState = AudioRecorderState.Ready;

get MediaPlugin(): MediaPlugin {
if (this.mediaPlugin == null) {
this.mediaPlugin = new MediaPlugin(’…/Library/NoCloud/recording.wav’);
}

return this.mediaPlugin;

}

startRecording() {
this.MediaPlugin.startRecord();
this.state = AudioRecorderState.Recording;
}

stopRecording() {
this.MediaPlugin.stopRecord();
this.state = AudioRecorderState.Recorded;
}

startPlayback() {
this.MediaPlugin.play();
this.state = AudioRecorderState.Playing;
}

stopPlayback() {
this.MediaPlugin.stop();
this.state = AudioRecorderState.Ready;
}
}

my ts is

import { Component } from ‘@angular/core’;
import { NavController, AlertController } from ‘ionic-angular’;
import { AudioRecorder, AudioRecorderState } from ‘…/…/services/audiorecorder’;

@Component({
templateUrl: ‘build/pages/home/home.html’,
providers: [AudioRecorder]
})
export class HomePage {
AudioRecorderState = AudioRecorderState;

constructor(public navCtrl: NavController,
public alertCtrl: AlertController,
public audioRecorder: AudioRecorder) {
}

startRecording() {
try {
this.audioRecorder.startRecording();
}
catch (e) {
this.showAlert(‘Could not start recording.’);
}
}

stopRecording() {
try {
this.audioRecorder.stopRecording();
}
catch (e) {
this.showAlert(‘Could not stop recording.’);
}
}

startPlayback() {
try {
this.audioRecorder.startPlayback();
}
catch (e) {
this.showAlert(‘Could not play recording.’);
}
}

stopPlayback() {
try {
this.audioRecorder.stopPlayback();
}
catch (e) {
this.showAlert(‘Could not stop playing recording.’);
}
}

showAlert(message) {
let alert = this.alertCtrl.create({
title: ‘Error’,
subTitle: message,
buttons: [‘OK’]
});
alert.present();
}
}

my html is only fourt button for action defined in provider

2 Likes