app.component.html
1 2 |
<button (click)="startPlay()">start</button> <button (click)="stopPlay()">play</button> |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import { Component, OnInit } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import Recorder from 'recorder-js'; declare var MediaRecorder: any; const httpOptions = { headers: new HttpHeaders({ 'responseType' : 'blob as json', 'Content-Type': 'application/json', 'Accept': 'application/json' }) }; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.css' ] }) export class AppComponent implements OnInit { blobFile; recordAudio; sendObj = { audio: this.blobFile }; audioContext = new (AudioContext)({sampleRate: 16000}); recorder = new Recorder(this.audioContext, {}); constructor(private http: HttpClient) {} ngOnInit() { this.recordAudio = () => { return new Promise(resolve => { navigator.mediaDevices.getUserMedia({ audio: true }) .then(stream => { const mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm', numberOfAudioChannels: 1, audioBitsPerSecond : 16000, }); const audioChunks = []; mediaRecorder.addEventListener("dataavailable", event => { audioChunks.push(event.data); }); const start = () => { mediaRecorder.start(); }; const stop = () => { return new Promise(resolve => { mediaRecorder.addEventListener('stop', () => { const audioBlob = new Blob(audioChunks, { 'type' : 'audio/wav; codecs=MS_PCM' }); const reader = new FileReader(); reader.readAsDataURL(audioBlob); reader.addEventListener('load', () => { const base64data = reader.result; this.sendObj.audio = base64data; // this.http.post('apiUrl', this.sendObj, httpOptions).subscribe(data => console.log(data)); }, false); const audioUrl = URL.createObjectURL(audioBlob); console.log('Audiourl', audioUrl); const audio = new Audio(audioUrl); const play = () => { audio.play(); }; resolve({ audioBlob, audioUrl, play }); }); mediaRecorder.stop(); }); }; resolve({ start, stop }); }); }); }; } async startPlay() { this.recorder = await this.recordAudio(); this.recorder.start(); } async stopPlay() { const audio = await this.recorder.stop(); audio.play(); } } |