Record an Audio


#1

Hi,

I want a page what can record audio with cordova plugin.

Here it is created file for record this function;

import { Component, Provider } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Media, MediaObject } from '@ionic-native/media';
import { File } from '@ionic-native/file';
import { Platform } from 'ionic-angular/platform/platform';
import { AudioProvider } from 'ionic-audio';

/**
 * Generated class for the SendPostPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-send-post',
  templateUrl: 'send-post.html',
})
export class SendPostPage {

  recording: boolean = false;
  filePath: string;
  fileName: string;
  audio: MediaObject;
  audioList: any[] = [];
  selectedTrack: any;

  mediaPlugin: Media = null;

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    private media: Media,
    private file: File,
    public platform: Platform,
    private audioProvide: AudioProvider) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SendPostPage');
  }

  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() + '.m4a';

      alert(this.file.tempDirectory)

      this.file.createFile(this.file.tempDirectory, this.filePath, true).then(() => {
        let file = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + this.filePath);
        alert(file);
      });

      if (this.file.documentsDirectory !== null || this.file.documentsDirectory !== undefined) {
        alert(this.file.documentsDirectory);
        this.audio = this.media.create(this.filePath);
      }
    }
    else if (this.platform.is('android')) {

      alert('Android');
      alert(this.file.tempDirectory);      

      this.fileName = 'record' + new Date().getDate() + new Date().getMonth() + new Date().getFullYear() + new Date().getHours() + new Date().getMinutes() + new Date().getSeconds() + '.3gp';

      this.file.createFile(this.file.tempDirectory, this.filePath, true).then(() => {
        let file = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + this.filePath);
      });

      if (this.file.documentsDirectory !== null || this.file.documentsDirectory !== undefined) {
        alert(this.file.documentsDirectory);
        this.audio = this.media.create(this.filePath);
      }
    }

    this.audio.startRecord();
    this.recording = true;
  }

  stopRecord() {
    this.audio.stopRecord();
    let data = { filename: this.fileName, src: this.filePath };
    this.audioList.push(data);
    localStorage.setItem("audiolist", JSON.stringify(this.audioList));
    this.recording = false;
  }

  ngAfterContentInit() {
    // get all tracks managed by AudioProvider so we can control playback via the API
    this.audioList = this.audioProvide.tracks;
  }

  playSelectedTrack() {
    // use AudioProvider to control selected track 
    this.audioProvide.play(this.selectedTrack);
  }

  pauseSelectedTrack() {
    // use AudioProvider to control selected track 
    this.audioProvide.pause(this.selectedTrack);
  }
}


So I want create an temporary file directory because I will send this file to server, user must take a check sound then post it. But temporary file return null.

How can do that?

EDIT

I need to know, should I create a directory for this function with cordova plugin? If it’s “Yes”, so

this.file.temporartDirectory

Must be seted?


#2

Did you find any solution?