Hello, i am trying to do an Ionic app and i am getting an error. Any suggestion?
Runtime error
this.file.documentsDirectory is null
[194]/HomePage.prototype.startRecord@http://localhost:8100/build/main.js:80:13
View_HomePage_2/<@ng:///AppModule/HomePage.ngfactory.js:36:19
handleEvent@http://localhost:8100/build/vendor.js:13608:132
callWithDebugContext@http://localhost:8100/build/vendor.js:15093:39
debugHandleEvent@http://localhost:8100/build/vendor.js:14680:12
dispatchEvent@http://localhost:8100/build/vendor.js:10057:16
renderEventHandlerClosure/<@http://localhost:8100/build/vendor.js:10671:38
decoratePreventDefault/<@http://localhost:8100/build/vendor.js:36353:53
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649
onInvokeTask@http://localhost:8100/build/vendor.js:4973:24
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15562
F</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815
F</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16787
p@http://localhost:8100/build/polyfills.js:2:27646
v@http://localhost:8100/build/polyfills.js:2:27893
home.html
<ion-header>
<ion-navbar>
<ion-title>
Sound Recorder & Player
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-card>
<ion-card-content>
<ion-card-title>
<button ion-button primary (click)="stopRecord()" *ngIf="recording"><ion-icon name="mic-off"></ion-icon> Stop Record</button>
<button ion-button primary (click)="startRecord()" *ngIf="!recording"><ion-icon name="mic"></ion-icon> Start Record</button>
</ion-card-title>
</ion-card-content>
</ion-card>
<ion-list>
<ion-item *ngFor="let audio of audioList; index as i;">
<p>{{audio.filename}}</p>
<button ion-button clear item-end large (click)="playAudio(audio.filename, i)"><ion-icon name="play"></ion-icon></button>
</ion-item>
</ion-list>
</ion-content>
home.ts
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { Media, MediaObject } from '@ionic-native/media';
import { File } from '@ionic-native/file';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController,
private media: Media,
private file: File,
public platform: Platform) {}
recording: boolean = false;
filePath: string;
fileName: string;
audio: MediaObject;
audioList: any[] = [];
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);
}
}