Problem in designing audio recorder app


#1

Hi
I am trying to design a audio recorder app with record, save, and playback functions, I could able to save and record my audio, and able to see it using file explorer in android device, I could not make a play back function it just gives me error code 1 NOT_FOUND_ERR but the audio file is inside my internal memory. could someone help me to fix this

  import { Component } from '@angular/core';
  import {File} from 'ionic-native';
  import { NavController,AlertController  } from 'ionic-angular';
  import { MediaPlugin } from 'ionic-native';
  declare var cordova;
  declare var cordovaFile; 
  @Component({
    selector: 'page-home',
    templateUrl: 'home.html'
 })
 export class HomePage {
    media: MediaPlugin = new MediaPlugin('../Library/NoCloud/recording.wav');

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

    createFolder(){
        console.log('inside folder creation');
        File.createDir(cordova.file.externalRootDirectory, "Audio_folder", false)
              .then(function (success) {
                console.log("folder creation sucess",success);
              }, function (error) {
                 console.log("folder creation error",error);
              });
    }

    startRecording() {
        try {           
            this.media.startRecord();           
            console.log('started to record');
        }       
        catch (e) {
            this.showAlert('Could not start recording.');
        }
    }
     stopRecording() {
        try {
            this.media.stopRecord();
            console.log("media time",this.media.getDuration());
            console.log('record stopped');      
        }
        catch (e) {
            this.showAlert('Could not stop recording.');
        }


    }

    startPlayback() { 
        try {
            this.media.play();
            console.log('in start play back function');
            console.log('in start play back function',this.media.play);
        }
        catch (e) {
            this.showAlert('Could not play recording.');
        }
    }

    stopPlayback() {console.log('playback stopped');
        try {
            this.media.stop();
        }
        catch (e) {
            this.showAlert('Could not stop playing recording.');
        }
    }
    showAlert(message) {
        let alert = this.alertCtrl.create({
            title: 'Error',
            subTitle: message,
            buttons: ['OK']
        });
        alert.present();
    }    }

How to record audio in Ionic 2
#2

I can’t see where you have saved the recorded audio… have only seen your createFolder() function

The MediaPlugin instance is created new when the class is constructed. At that point, the native media plugin will not yet have been loaded, so creating the MediaPlugin instance throws an error.
initialize media within the lifestyle evnt for ionic component.


ionViewDidEnter{
    media: MediaPlugin = new MediaPlugin('../Library/NoCloud/recording.wav');
}

Look at the lifecycle events for an Ionic component (in order of occurance):

ionViewLoaded: runs when the page has loaded.
ionViewWillEnter: runs when the page is about to enter and become the active page.
ionViewDidEnter: runs when the page has fully entered and is now the active page.
ionViewWillLeave: runs when the page is about to leave and no longer be the active page.
ionViewDidLeave: runs when the page has finished leaving and is no longer the active page.
ionViewWillUnload: runs when the page is about to be destroyed and have its elements removed.
ionViewDidUnload: runs after the page has been destroyed and its elements have been removed.