Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- //import { MediaCapture } from '@ionic-native/media-capture/ngx';
- //import { IonicStorageModule } from '@ionic/storage';
- import { Media, MediaObject } from '@ionic-native/media/ngx';
- import { File } from '@ionic-native/file/ngx';
- // import { Storage } from '@ionic/storage';
- import { Platform } from '@ionic/angular'
- @Component({
- selector: 'app-audios',
- templateUrl: './audios.page.html',
- styleUrls: ['./audios.page.scss'],
- })
- export class AudiosPage implements OnInit {
- constructor( private media: Media, private file: File, public platform: Platform) {
- }
- recording: boolean = false;
- filePath: string;
- fileName: string;
- nome: string;
- audio: MediaObject;
- audioList: any[] = [];
- ngOnInit() {
- }
- getAudioList() {
- if(localStorage.getItem("audiolist")) {
- this.audioList = JSON.parse(localStorage.getItem("audiolist"));
- console.log(this.audioList);
- }
- }
- ionViewWillEnter() {
- this.getAudioList();
- }
- 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()+'.3gp';
- this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + this.fileName;
- this.audio = this.media.create(this.filePath);
- } else if (this.platform.is('android')) {
- this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new Date().getSeconds()+'.3gp';
- this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '') + this.fileName;
- this.audio = this.media.create(this.filePath);
- }
- this.audio.startRecord();
- this.recording = true;
- }
- stopRecord() {
- this.audio.stopRecord();
- let data = { filename: this.fileName };
- this.audioList.push(data);
- localStorage.setItem("audiolist", JSON.stringify(this.audioList));
- this.recording = false;
- this.getAudioList();
- }
- playAudio(file,idx) {
- if (this.platform.is('ios')) {
- this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + file;
- this.audio = this.media.create(this.filePath);
- } else if (this.platform.is('android')) {
- this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '') + file;
- this.audio = this.media.create(this.filePath);
- }
- this.audio.play();
- this.audio.setVolume(0.8);
- }
- deleteAudio(){
- if (this.platform.is('ios')) {
- this.file.removeFile(this.file.documentsDirectory, this.fileName).then(result => console.log(result), error => console.warn(error))
- // this.file.removeFile(this.filePath, '')
- } else if (this.platform.is('android')) {
- this.file.removeFile(this.file.externalDataDirectory, this.fileName).then(result => console.log(result), error => console.warn(error))
- // this.file.removeFile(this.filePath, '')
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement