0

I am trying to record audio and save it to a specific folder and make some playback functionality with that saved audio. i could record and save it, but i could not save that audio file to a directory and could not able to make playback could someone help me

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();
    }

}

Currently i could able to make a folder into my mobile internal storage and i could able to see it using my file explorer and the recorded audio file is also visible in my internal storage, but i could not make a playback i get an error code 1 NOT_FOUND_ERR. And if i try to get duration i get -1 in my console And i am testing it in android device Thanks for your help in advance

1

2 Answers 2

0

Audio recorder on android use:

public media: MediaPlugin;

 startRecording() {
     try { 
         this.media = new MediaPlugin(“recording.mp3”);       
         this.media.startRecord();          
         console.log('started to record');
         }    
   catch (e) {
         this.showAlert('Could not start recording.');
         }
 }
1
  • thanks but could you make a play back of the recorded audio and also rename it @SOLER Jan 30, 2017 at 4:16
0

I give an answer here to your main question. I think if you will follow those guides your application will works.

The getDuration() function will always show you -1 if you check it without starting the playback. So, if you want to check the duration of music, you have to start playback.

var durationInterval = window.setInterval(() => {
  if (this.mediaPlugin) {
    this.durationOfMusic = this.mediaPlugin.getDuration().toFixed(2);
  } 

  if (this.durationOfMusic > -1) {        
    window.clearInterval(durationInterval);
  }
}, 1);    
3
  • could i get the media time without making playback Mar 13, 2017 at 6:11
  • Unfortunately with this plugin i don't think so. My example is not the best, but i start the playback with mute (setVolume(0)), and then create an interval where i getDuration and update it every ms, until the duration < 0, then i start the music from the beginning with volume(1). However you can measure the time at recording with interval, but that isn't the best way (definitely not the best).
    – Hexiler
    Mar 13, 2017 at 16:50
  • Is there any other way were we could able to record audio make playback, get its duration and the file size of recorded audio Mar 14, 2017 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.