I am working on project where I need to record audio and allow user to stop when he is done recording. I want to upload this recorded audio to firebase storage. I am not sure how to do it.
I am sharing the stack blitz link where I have implemented the audio recorder functionality.
https://stackblitz.com/edit/angular-record-rtc-demo
I have created one download function like below
_downloadFile(data: any, type: string, filename: string): any {
const blob = new Blob([data], { type: type });
const url = window.URL.createObjectURL(blob);
//this.video.srcObject = stream;
//const url = data;
const anchor = document.createElement('a');
anchor.download = filename;
anchor.href = url;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}
I have created the fucntion for uploading a file like below
selectFile(event): void {
this.isUploading = true
this.percentage = 0
const file = event.target.files[0];
this.currentFileUpload = new FileUpload(file);
const filePath = `voice`;
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(`voice`, file);
task
.snapshotChanges()
.pipe(
finalize(() => {
this.downloadURL = fileRef.getDownloadURL();
this.downloadURL.subscribe(url => {
console.log(url)
if (url) {
this.isUploading = false
this.fb = url;
}
this.addUrlToEditor(url)
task.percentageChanges().subscribe(percentage => {
this.percentage = Math.round(percentage);
this.percentage = percentage
})
});
})
)
.subscribe(url => {
if (url) {
this.selectedFiles = null;
}
});
}
I am able to download the audio. But I also want to upload recorded audio to firebase storage and use that url for future refrence for downloading the audio and other stuff. Any help will be appreciated.
You can use this.audioName and this.audioBlob :
const storage = getStorage(); // imported from firebase/storage
const storageRef:any = ref(storage, this.audioName);
uploadBytes(storageRef, this.audioBlob);
This solution assumes that you have already configured Firebase in your Angular Project.