Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. //import { MediaCapture } from '@ionic-native/media-capture/ngx';
  3. //import { IonicStorageModule } from '@ionic/storage';
  4. import { Media, MediaObject  } from '@ionic-native/media/ngx';
  5. import { File } from '@ionic-native/file/ngx';
  6. // import { Storage } from '@ionic/storage';
  7. import { Platform } from '@ionic/angular'
  8.  
  9. @Component({
  10.   selector: 'app-audios',
  11.   templateUrl: './audios.page.html',
  12.   styleUrls: ['./audios.page.scss'],
  13. })
  14. export class AudiosPage implements OnInit {
  15.  
  16.   constructor( private media: Media, private file: File, public platform: Platform) {
  17.  
  18.   }
  19.  
  20.   recording: boolean = false;
  21.   filePath: string;
  22.   fileName: string;
  23.   nome: string;
  24.   audio: MediaObject;
  25.   audioList: any[] = [];
  26.  
  27.  
  28.  
  29.   ngOnInit() {
  30.   }
  31.  
  32.   getAudioList() {
  33.     if(localStorage.getItem("audiolist")) {
  34.       this.audioList = JSON.parse(localStorage.getItem("audiolist"));
  35.       console.log(this.audioList);
  36.     }
  37.   }
  38.  
  39.   ionViewWillEnter() {
  40.     this.getAudioList();
  41.   }
  42.  
  43.  
  44.   startRecord() {
  45.     if (this.platform.is('ios')) {
  46.       this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new Date().getSeconds()+'.3gp';
  47.        this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + this.fileName;
  48.          this.audio = this.media.create(this.filePath);
  49.  
  50.     } else if (this.platform.is('android')) {
  51.       this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new Date().getSeconds()+'.3gp';
  52.         this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '') + this.fileName;
  53.          this.audio = this.media.create(this.filePath);
  54.     }
  55.     this.audio.startRecord();
  56.     this.recording = true;
  57.   }
  58.  
  59.   stopRecord() {
  60.     this.audio.stopRecord();
  61.       let data = { filename: this.fileName };
  62.     this.audioList.push(data);
  63.      localStorage.setItem("audiolist", JSON.stringify(this.audioList));
  64.     this.recording = false;
  65.     this.getAudioList();
  66.   }
  67.  
  68.   playAudio(file,idx) {
  69.     if (this.platform.is('ios')) {
  70.       this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + file;
  71.        this.audio = this.media.create(this.filePath);
  72.  
  73.     } else if (this.platform.is('android')) {
  74.       this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '') + file;
  75.         this.audio = this.media.create(this.filePath);
  76.     }
  77.     this.audio.play();
  78.     this.audio.setVolume(0.8);
  79.   }
  80.  
  81.  
  82.   deleteAudio(){
  83.     if (this.platform.is('ios')) {
  84.      this.file.removeFile(this.file.documentsDirectory, this.fileName).then(result => console.log(result), error => console.warn(error))
  85.    // this.file.removeFile(this.filePath, '')
  86.  
  87.    } else if (this.platform.is('android')) {
  88.     this.file.removeFile(this.file.externalDataDirectory, this.fileName).then(result => console.log(result), error => console.warn(error))
  89.    // this.file.removeFile(this.filePath, '')
  90.     }
  91.    }
  92.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement