4

I'm trying to record audio via the microphone in the browser and then save it to file server side using Node and a binary server. I am using this tutorial as a guide but am having trouble getting it to work with Angular2. Currently it appears to be saving some type of file to the server but it's not any type of playable audio. Here's all my code:

import { Component } from '@angular/core';
import { ContributorService } from './contributor.service';
import { CardDetailService } from './card-detail.service';
import { CardDetailComponent } from './card-detail.component';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
declare let navigator: any;
declare let MediaRecorder: any;
declare let BinaryClient: any;



@Component({
selector: 'contributor',
template: `
    <h1>Record Message</h1>

    <button (click)='start()'>Start</button>
    <button (click)='stop()'>Stop</button>
    <button (click)='play()'>Play</button>

    <a>{{hf}}</a>

    <button class="go-back" (click)="goBack()">Done</button>

`
})
export class ContributorComponent {
private chunks: any[] = [];
private recorder;
private audio;
private counter = 1;
private Stream;
private recording = false;

constructor(
    private contributorService: ContributorService,
    private cardDetailService: CardDetailService,
    private location: Location,
    private route: ActivatedRoute,
    private router: Router,
) {}

ngOnInit() {
    let client = new BinaryClient('ws://localhost:9001');

    client.on('open', function() {
        let Stream = client.createStream();

    });

    let audio = {
        tag: 'audio',
        type: 'audio/ogg',
        ext: '.ogg',
        gUM: {audio: true}
    };
    let context = new AudioContext();

    if (!navigator.getUserMedia) {
        navigator.getUserMedia = navigator.getUserMedia ||     navigator.webkitGetUserMedia ||
        navigator.mozGetUserMedia || navigator.msGetUserMedia;
    }

    if (navigator.getUserMedia) {
        navigator.getUserMedia({audio: true }, this.success,     function(e) {
            alert('Error capturing audio.');
        });
        } else {
            alert('getUserMedia not supported in this browser.');
        }
}

start() {
    this.recording = true;
}

stop() {
    this.recording = false;
    this.Stream.end();
}

recorderProcess(e) {
    let left = e.inputBuffer.getChannelData(0);
    this.Stream.write(this.convertFloat32ToInt16(left));
}

success(e) {
    let context = new AudioContext();

  // the sample rate is in context.sampleRate
    let audioInput = context.createMediaStreamSource(e);

    let recorder = context.createScriptProcessor(2048, 1, 1);

    this.recorder.onaudioprocess = function(e){
        if (!this.recording) { return; };
        console.log ('recording');
        var left = e.inputBuffer.getChannelData(0);
        this.Stream.write(this.convertoFloat32ToInt16(left));
    };

    audioInput.connect(this.recorder);
    this.recorder.connect(context.destination); 
}

convertFloat32ToInt16(buffer) {
    let l = buffer.length;
    let buf = new Int16Array(l);
    while (l--) {
        buf[l] = Math.min(1, buffer[l]) * 0x7FFF;
    }
    return buf.buffer;
}


goBack() {
    this.location.back();
}
}

and here are the errors it throws:

contributor.component.ts:96Uncaught TypeError: Cannot read property 'recorder' of undefined(…)ContributorComponent.success @ contributor.component.ts:96 core.umd.js:3004 EXCEPTION: Error in ./ContributorComponent class ContributorComponent - inline template:4:10 caused by: Cannot read property 'end' of undefinedErrorHandler.handleError

Linx
  • 753
  • 3
  • 15
  • 34
  • Looks like `this.recorder` is undefined. Nothing assigned to it. Maybe changing `let recorder = context.createScriptProcessor(2048, 1, 1);` to `this.recorder = context.createScriptProcessor(2048, 1, 1);` could help. Don't know what `context.createScriptProcessor(...)` is doing but with `let` you're only creating an unused variable. – AWolf Nov 09 '16 at 20:15
  • Thanks for the suggestion, @AWolf but I tried changing let recorder to this.recorder and it still gives the same errors – Linx Nov 09 '16 at 22:04
  • Another problem could be that `AudioContext` is not available. Try to inject window object like described [here](http://stackoverflow.com/questions/34177221/angular2-how-to-inject-window-into-an-angular2-service) and access it with `window.AudioContext`. – AWolf Nov 09 '16 at 23:11

0 Answers0